each of line 16, 17, 18 in attached file t.d
when comment off, will generate an error.
line 16: // wrong: t.A:- t.B:+ t.C:- int:-
line 17: // wrong: t.A:- t.B:- t.C:- int:-
line 18: // wrong: t.A:- t.B:- t.C:- int:-
I wonder if anyone ever used static if typeof at all.
Comment #1 by someanon — 2008-08-07T16:04:10Z
Created attachment 266
the source file
Comment #2 by 2korden — 2008-08-07T18:51:05Z
no need for posting twise, you already reported an issue earlier.
*** This bug has been marked as a duplicate of 2154 ***
Comment #3 by someanon — 2008-08-07T19:13:48Z
No, this is not DUPLICATE.
That one is that the compiler doesn't accept the code.
This one, is the binary generated behaves wrong.
Comment #4 by 2korden — 2008-08-07T19:42:25Z
I can't agree with you. The binary generated is perfectly valid. It never crashes, it behaves just like intended. The fact alone that you expect different result doesn't mean the executable is broken.
Your static if check is not passed, that's all.
If you expect to see the following output:
test.A:+ test.B:+ test.C:- int:-
then you should change your code. You are not allowed to access non-static data via class name like this:
bool result = is(typeof(E.toString()) : string); // false unless toString is static
or like this:
bool result = is(typeof(E.myInt) : int);
You have to create a class instance, first:
auto e = new E();
bool result = is(typeof(e.toString()) : string); // true for any class unless toString is private
or, better, in one line:
bool result = is(typeof((new E()).toString()) : string);
Note that no allocation will take place in latter case since it is merely a check.
Shall we consider an issue closed by now?
Comment #5 by someanon — 2008-08-07T20:33:48Z
(In reply to comment #4)
> I can't agree with you. The binary generated is perfectly valid. It never
> crashes, it behaves just like intended. The fact alone that you expect
> different result doesn't mean the executable is broken.
>
> Your static if check is not passed, that's all.
>
> If you expect to see the following output:
> test.A:+ test.B:+ test.C:- int:-
>
> then you should change your code. You are not allowed to access non-static data
> via class name like this:
> bool result = is(typeof(E.toString()) : string); // false unless toString is
> static
>
> or like this:
> bool result = is(typeof(E.myInt) : int);
>
> You have to create a class instance, first:
> auto e = new E();
> bool result = is(typeof(e.toString()) : string); // true for any class unless
> toString is private
>
> or, better, in one line:
> bool result = is(typeof((new E()).toString()) : string);
>
> Note that no allocation will take place in latter case since it is merely a
> check.
>
> Shall we consider an issue closed by now?
Thanks for your answer, but No. When you give an answer please test it: (with v2.017 on Linux)
If I change the test code as you suggested:
static if (is((new E()).toString()) : string) { // regardless of 'static'
$ dmd t.d
t.d(18): basic type expected, not (
t.d(18): found 'new' when expecting ')'
t.d(18): found 'E' when expecting ')'
t.d(18): found ')' when expecting ';' following 'statement'
t.d(18): found ':' instead of statement
t.d(20): Declaration expected, not 'else'
t.d(23): Declaration expected, not 'return'
t.d(24): unrecognized declaration
BTW, I need static test, because inside '{}' I will do something with the method/attribute.
Comment #6 by someanon — 2008-08-07T20:41:59Z
sorry I got it wrong: it should be static if (is(typeof((new E()).toString()) : string)) {..}
Now the code compiles, and runs correctly.
Thanks for the reply.
Comment #7 by someanon — 2008-08-07T21:23:17Z
Hold on, one more issue: in the original file
line 16: // wrong: t.A:- t.B:+ t.C:- int:-
Why an empty class B{} pass the test? it has a static toString?
Comment #8 by 2korden — 2008-08-08T06:13:08Z
Yes, it inherits it from Object.
Comment #9 by someanon — 2008-08-08T23:39:11Z
(In reply to comment #8)
> Yes, it inherits it from Object.
>
/dmd/src/phobos/object.d
class Object
{
void print();
string toString(); // you mean this one?
hash_t toHash();
int opCmp(Object o);
bool opEquals(Object o);
final void notifyRegister(void delegate(Object) dg);
final void notifyUnRegister(void delegate(Object) dg);
static Object factory(string classname);
}
How it pass the static test in my original code?
Shall we reopen the bug? ;-)
Comment #10 by someanon — 2008-08-10T15:09:19Z
line 16: // wrong: t.A:- t.B:+ t.C:- int:-
Why an empty class B{} pass the test?
Comment #11 by clugdbug — 2009-11-23T00:29:07Z
(In reply to comment #10)
> line 16: // wrong: t.A:- t.B:+ t.C:- int:-
>
> Why an empty class B{} pass the test?
Because it has toString(). Try this code:
---
import std.stdio;
class B {}
void main()
{
B b = new B;
writefln(b.toString());
}
---
There's no bug here.