Can you explain this?
Jonathan M Davis via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Aug 20 15:06:20 PDT 2014
On Wednesday, 20 August 2014 at 21:06:49 UTC, monarch_dodra wrote:
> On Wednesday, 20 August 2014 at 20:39:42 UTC, Jonathan M Davis
> wrote:
>> is(typeof(foo)) and __traits(compiles, foo) are not the same.
>> The first tests for the existence of the symbol, whereas the
>> second checks whether the code will actually compile.
>
> Is that even true? I mean, are you just repeating something
> you've heard, or have you checked? "is(typeof(foo))" has always
> failed for me merelly if "foo" fails to compile. "foo" being an
> existing (but private) symbol is enough.
>
> Test case:
> //----
> module foo;
>
> struct S
> {
> private int i;
> }
> //----
> import foo;
>
> void main(string[] args)
> {
> S s;
> writeln(is(typeof(s.i)));
> writeln(__traits(compiles, s.i));
> }
> //----
>
> This says false, false.
>
> I've yet to find a usecase where "is(typeof(...))" and
> "__traits(compiles, ...)" aren't interchangeable.
>
> I mean, I may have missed something, but it seems the whole
> "private" thing is just miss-information.
Well, hereas an example of them not being the same:
-------
import std.stdio;
struct S
{
static void foo()
{
writeln(is(typeof(this)));
writeln(__traits(compiles, this));
}
}
void main()
{
S.foo();
}
-------
I originally found out about it from Don here:
https://issues.dlang.org/show_bug.cgi?id=8339
I don't know why your example doesn't show them as different. But
we should probably change it so that they _are_ the same - either
that or document their differences explicitly and clearly, but I
don't know why the is(typeof.. behavior here would be desirable.
Maybe so that we could do type(this) to declare a local variable
of the class type generically? I don't know. They're _almost_ the
same but not quite, and I don't know what the exact differences
are. Pretty much all I have to go on is Don's explanation in that
bug report.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list