Bug 2873 – typeof() for member functions evaluated incorrectly
Status
RESOLVED
Resolution
INVALID
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Windows
Creation time
2009-04-21T20:12:00Z
Last change time
2015-06-09T01:26:26Z
Keywords
wrong-code
Assigned to
nobody
Creator
dsimcha
Comments
Comment #0 by dsimcha — 2009-04-21T20:12:49Z
// Copied/pasted from std.range, added pragma.
template hasLength(R)
{
pragma(msg, typeof(R.init.length).stringof); // (uint())()
enum bool hasLength = is(typeof(R.init.length) : ulong);
}
struct Foo {
uint length() {
return 0;
}
}
const Foo foo;
static assert(hasLength!(Foo)); // Fails.
This is pretty serious because it makes important functionality in the new Phobos unusable. However, an easy workaround to fix Phobos until the underlying DMD bug gets fixed is:
enum bool hasLength = is(typeof(R.init.length) : ulong) ||
is(typeof(R.init.length()) : ulong);
Also, what the heck is a (uint())() ?
Comment #1 by repeatedly — 2010-05-02T10:04:17Z
*** Issue 3508 has been marked as a duplicate of this issue. ***
Comment #2 by repeatedly — 2010-05-04T17:52:04Z
Since adding @property, this behavior isn't a bug. length method should be a property. Please use @property for length method.
Comment #3 by bearophile_hugs — 2010-05-04T18:30:17Z
Masahiro Nakagawa, I am not sure I understand this bug report and your answer well. But if I understand them, then then if you are right, and length method should be a property, then the compiler has to give a compile time error if you try to define a non-property length method, otherwise it's too much easy to write a bug.
Comment #4 by repeatedly — 2010-05-04T20:33:06Z
(In reply to comment #3)
> Masahiro Nakagawa, I am not sure I understand this bug report and your answer
> well.
Old D's property is a syntactic sugar(member function automatically becomes property), so typeof() should have evaluated .length and .length() correctly. But now, D has @property. User can choose property or non-property.
> But if I understand them, then then if you are right, and length method
> should be a property, then the compiler has to give a compile time error if you
> try to define a non-property length method, otherwise it's too much easy to
> write a bug.
I agree because I can't image non-property length. I think D's compiler should print warning message if user tries to define non-property length.