The Expressiveness of D

Simen kjaeraas simen.kjaras at gmail.com
Tue Nov 2 11:13:51 PDT 2010


Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:

>> I believe it has been discussed numerous times before that the ?:
>> test should be used to find the element type - not sure why it
>> isn't.
>
> Looks like outdated documentation to me.

Well, it certainly doesn't behave as it should, if %u's post is
anything to go by. (and my testing of the same issue - it's not
just %u) Filed as 5156.


> This was already contributed to in Phobos (under a different syntax) but  
> I rejected it on grounds of safety: the value built by "_" must escape  
> addresses of all of its arguments, so it's not safe.

True. Bummer. Hm, now, the intent of _ is that the constructed type should
exist only as a temporary, I believe.

Maybe it is possible to do this in a safer way. I actually have a solution,
which I have posted here before, and I feel might be worthy of an
enhancement request: that templates may be used as pseudo-types, in that a
operator overloading should be allowed on non-type template instances:



import std.typecons;
import std.typetuple;
import dranges.templates;
import dranges.typetuple;

template _( T... ) if ( allSatisfy!( isAlias, T ) ) {
     Tuple!( StaticMap!( TypeOf, T ) ) opAssign( Tuple!( StaticMap!(  
TypeOf, T ) ) args ) {
         foreach ( i, e; T ) {
             e = args[i];
         }
         return args;
     }
}


unittest {
     int a = 1; b = 1;
     _!( a, b ) = tuple( b, a+b ); // Equivalent to _!( a, b ).opAssign( b,  
a+b );
}

Sadly, this currently does not work, not just due to the aforementioned
limitation, but also because the reassigned values are discarded as the
function returns. This latter part is confusing me. Is this intended
behavior?

A different solution might be @ephemeral, marking types that are not
supposed to be stored anywhere, and statically disallowing their use
as variables.


> My suggestion to address this issue was (and is) to make the assigned  
> part of the function call:
>
> int a, b;
> scatter(tuple(b, a), a, b);
> scatter(tuple(b, a + b), a, b);
> int[] arr = [0, 1, 2, 3, 4];
> int[] c;
> scatter(arr, a, b, c);

That certainly works, but it is not as prettiful or straightforward.


-- 
Simen


More information about the Digitalmars-d mailing list