www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - __traits(getMember) and uniform call syntax

reply Tomek =?UTF-8?B?U293acWEc2tp?= <just ask.me> writes:
What's the status quo on member functions defined outside the type?

import std.array;

void main() {
    int[] arr = [1,2];
    
    // compiles, should it?
    int a = __traits(getMember, arr, "front");

    // compiles (called popFront), should it?
    __traits(getMember, arr, "popFront");

    // doesn't compile
//     __traits(getMember, arr, "popFront")();
}


I'd appreciate if someone told apart bugs from features.

-- 
Tomek
Oct 16 2010
next sibling parent Tomek =?UTF-8?B?U293acWEc2tp?= <just ask.me> writes:
Tomek Sowiński napisał:

 What's the status quo on member functions defined outside the type?
 
 import std.array;
 
 void main() {
     int[] arr = [1,2];
     
     // compiles, should it?
     int a = __traits(getMember, arr, "front");
 
     // compiles (called popFront), should it?
     __traits(getMember, arr, "popFront");
 
     // doesn't compile
 //     __traits(getMember, arr, "popFront")();
 }
 
 
 I'd appreciate if someone told apart bugs from features.
The latter two are compiler bugs to me, __traits(getMember, ...)() should work, but please shed some light regardless. -- Tomek
Oct 16 2010
prev sibling parent Jonathan M Davis <jmdavisProg gmx.com> writes:
On Saturday 16 October 2010 17:03:51 Tomek Sowi=C5=84ski wrote:
 What's the status quo on member functions defined outside the type?
=20
 import std.array;
=20
 void main() {
     int[] arr =3D [1,2];
=20
     // compiles, should it?
     int a =3D __traits(getMember, arr, "front");
=20
     // compiles (called popFront), should it?
     __traits(getMember, arr, "popFront");
=20
     // doesn't compile
 //     __traits(getMember, arr, "popFront")();
 }
=20
=20
 I'd appreciate if someone told apart bugs from features.
Uniform function syntax doesn't change the member functions of a type. It j= ust=20 allows you to call functions as if they were member functions. And how coul= d it=20 be otherwise? Every time you imported a new module, it could completely cha= nge=20 the set of member functions for any types in the current module. That would= be=20 really bad. Not to mention, all of the code that relies on knowing what the= =20 member functions actually are would be screwed if uniform function syntaxt= =20 affected __traits(getMember ...). Arrays don't have member functions. getMember _might_ give you properties l= ike=20 length (I'd have to try it to know for sure), but it has no member function= s, so=20 of course it isn't going to find popFront() and the like in its member func= tions. If you want that sort of behaviour, use __traits(compiles, ...) instead. Uniform function syntax is syntactic sugar. It should not change the meanin= g of=20 a program like it would if you tried to make them member functions accordin= g to=20 __traits. =2D Jonathan M Davis
Oct 16 2010