www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - isRef, isLazy, isOut

reply Eldar Insafutdinov <e.insafutdinov gmail.com> writes:
Following the commits to the dmd repository I found these traits implemented.
The idea is good, but the API chosen does not look right.  Why not have
__traits(storageClass, symbol) and a enum which defines the return value:
enum StorageClass {
    None,
    Ref,
    Lazy,
    Out
}

One could write templates isRef, isLazy etc on top of that if he/she really
needs. IMO this is a cleaner way to implement this particular feature.
Dec 19 2009
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Eldar Insafutdinov wrote:
 Following the commits to the dmd repository I found these traits implemented.
The idea is good, but the API chosen does not look right.  Why not have
__traits(storageClass, symbol) and a enum which defines the return value:
 enum StorageClass {
     None,
     Ref,
     Lazy,
     Out
 }
 
 One could write templates isRef, isLazy etc on top of that if he/she really
needs. IMO this is a cleaner way to implement this particular feature.

I asked in vain for is(var == ref), is(var == out) etc. Andrei
Dec 19 2009
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Andrei Alexandrescu wrote:
 I asked in vain for is(var == ref), is(var == out) etc.

That's because 'is' works with types, not symbols. It didn't fit.
Dec 19 2009
next sibling parent reply Chris Nicholson-Sauls <ibisbasenji gmail.com> writes:
Walter Bright wrote:
 Andrei Alexandrescu wrote:
 I asked in vain for is(var == ref), is(var == out) etc.

That's because 'is' works with types, not symbols. It didn't fit.

is(typeof(var) == ref) ? -- Chris Nicholson-Sauls
Dec 20 2009
parent reply Walter Bright <newshound1 digitalmars.com> writes:
Chris Nicholson-Sauls wrote:
 Walter Bright wrote:
 Andrei Alexandrescu wrote:
 I asked in vain for is(var == ref), is(var == out) etc.

That's because 'is' works with types, not symbols. It didn't fit.

is(typeof(var) == ref) ?

No, because ref is not part of the type. It's a storage class.
Dec 20 2009
parent reply Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
Walter Bright wrote:
 Chris Nicholson-Sauls wrote:
 Walter Bright wrote:
 Andrei Alexandrescu wrote:
 I asked in vain for is(var == ref), is(var == out) etc.

That's because 'is' works with types, not symbols. It didn't fit.

is(typeof(var) == ref) ?

No, because ref is not part of the type. It's a storage class.

Speaking of isLazy - any fresh ideas on dissing lazy? FWIW we can replace it with the conversion of expressions to delegates. The resulting situation is liable to a few ambiguities but is much better than lazy. Andrei
Dec 20 2009
parent Kasumi Hanazuki <k.hanazuki gmail.com> writes:
(2009/12/21 4:43), Andrei Alexandrescu wrote:
 Speaking of isLazy - any fresh ideas on dissing lazy? FWIW we can
 replace it with the conversion of expressions to delegates. The
 resulting situation is liable to a few ambiguities but is much better
 than lazy.

Nobody seemed to mention in the last thread, but D already has the implicit expression-to-delegate conversion in the type-safe variadic parameters. (only available in the last part of parameters though...) Can we merge lazy with it? ---- void f(void delegate()[1] gs...) { gs[0](); gs[0](); } void g() { writeln(42); } void main() { f(g()); }
Dec 20 2009
prev sibling parent reply Max Samukha <spambox d-coding.com> writes:
On 19.12.2009 23:00, Walter Bright wrote:
 Andrei Alexandrescu wrote:
 I asked in vain for is(var == ref), is(var == out) etc.

That's because 'is' works with types, not symbols. It didn't fit.

Will there be a way to get storage classes from function symbols or types? Currently, we have to use a Signature template that, along with parameter and return types, extracts storage classes by parsing the value of parameter type tuple's stringof. Ugly and unreliable. We need a way to do it without resorting to hacks. Something in this vein: enum StorageClasses { ... } class A { ref int foo(const ref int a, lazy int b) const; void bar(out a); } assert(__traits(storageClasses, A.foo) == StorageClasses.Const | StorageClasses.Ref); assert(__traits(parameterStorageClasses, A.foo) == [StorageClasses.Const | StorageClasses.Ref, StorageClasses.Lazy]); See also http://d.puremagic.com/issues/show_bug.cgi?id=1818
Dec 20 2009
parent Eldar Insafutdinov <e.insafutdinov gmail.com> writes:
Max Samukha Wrote:

 On 19.12.2009 23:00, Walter Bright wrote:
 Andrei Alexandrescu wrote:
 I asked in vain for is(var == ref), is(var == out) etc.

That's because 'is' works with types, not symbols. It didn't fit.

Will there be a way to get storage classes from function symbols or types? Currently, we have to use a Signature template that, along with parameter and return types, extracts storage classes by parsing the value of parameter type tuple's stringof. Ugly and unreliable. We need a way to do it without resorting to hacks. Something in this vein: enum StorageClasses { ... } class A { ref int foo(const ref int a, lazy int b) const; void bar(out a); } assert(__traits(storageClasses, A.foo) == StorageClasses.Const | StorageClasses.Ref); assert(__traits(parameterStorageClasses, A.foo) == [StorageClasses.Const | StorageClasses.Ref, StorageClasses.Lazy]); See also http://d.puremagic.com/issues/show_bug.cgi?id=1818

Please, please...
Dec 20 2009