[Issue 16642] New: byCodeUnit doesn't work AutodecodableStrings unless they're actually strings or alias a variable that's a string
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Thu Oct 27 01:09:44 PDT 2016
https://issues.dlang.org/show_bug.cgi?id=16642
Issue ID: 16642
Summary: byCodeUnit doesn't work AutodecodableStrings unless
they're actually strings or alias a variable that's a
string
Product: D
Version: D2
Hardware: All
OS: All
Status: NEW
Severity: normal
Priority: P1
Component: phobos
Assignee: nobody at puremagic.com
Reporter: issues.dlang at jmdavisProg.com
This code fails to compile
import std.utf;
struct S
{
string s;
string str() { return s; }
alias str this;
}
void main()
{
auto s = S("hello");
auto range = byCodeUnit(s);
}
Similarly, this fails to compile
import std.utf;
enum E { a = "hello" }
void main()
{
auto s = E.a;
auto range = byCodeUnit(s);
}
Also, this compiles but uses the alias rather than the range API and thus fails
the assertion:
import std.range.primitives;
import std.utf;
static struct RangeAndStringish
{
string data;
string s;
alias s this;
bool empty() { return data.empty; }
char front() { return data[0]; }
void popFront() { data = data[1 .. $]; }
}
void main()
{
auto fn = RangeAndStringish("test.d", "other");
auto x = fn.byCodeUnit();
assert(x.front == 't');
}
I suppose that it could be argued that this last case is doing the right thing
- or at least that it's ambiguous, but normally, an implicit conversion is only
supposed to be used if the type itself doesn't work directly, and in this case,
the type itself would work without the implicit conversion.
--
More information about the Digitalmars-d-bugs
mailing list