The Expressiveness of D
Lutger
lutger.blijdestijn at gmail.com
Tue Nov 2 07:29:42 PDT 2010
Simen kjaeraas wrote:
> %u <user at web.news> wrote:
>
>> class A {}
>> class B : A {}
>> class C : A {}
>>
>> template T(A...) { alias A T; }
>>
>> void main() {
>> auto a = true ? new B : new C;
>> // these don't work - why?
>> // auto b = [new B, new C];
>> // auto c = { return [1: new B,2: new C]; };
>
> "The type of the first element is taken to be the type of all the
> elements, and all elements are implicitly converted to that type."
> (http://digitalmars.com/d/2.0/expression.html#ArrayLiteral)
>
> 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.
It is stated in TDPL that the ?: test should be used, and it already works:
auto a = [1,2,3.5];
pragma(msg, typeof(a).stringof); // prints double[], not int[]
>
>> T!(int,int) e = (1,2);
>> e = T!(3,4);
>>
>> // ah - so (1,2) syntax on initialization, T!(1,2) when assigning!
>> T!(int,int) d = T!(1,2);
>>
>> e = d;
>>
>> // tuples aren't first class, why?
>> // auto f = { return e; };
>> }
>
> Compile-time argument tuples are something of a strange beast. They
> behave not very unlike tuples, but due to their ability to hold
> types, literals, expressions and aliases to whatever, they are not
> a very good match for what you'd expect tuples to be. (e.g, what do
> you expect T!(3,T) to be?)
>
> For tuples, you should instead look into std.typecons' Tuple struct
> and tuple function:
>
> Tuple!( int, "n", string, "s" ) tup;
> tup.n = 4;
> tup.s = "A string! My kingdom for a string!";
>
> auto tup2 = tuple( 1, 2 );
> assert( is( typeof( tup2 ) == Tuple!( int, int ) ) );
>
>
> For even better support of tuples, you should have a look-see at
> Philippe Sigaud's dranges.tuple and dranges.reftuple
> (http://svn.dsource.org/projects/dranges/trunk/dranges/docs/tuple.html
> and
> http://svn.dsource.org/projects/dranges/trunk/dranges/docs/reftuple.html)
>
> The latter is absolutely awesome, and should be made part of
> phobos ASAP, IMO (though according to the documentation, it
> is not in tip-top shape).
...
vote++
More information about the Digitalmars-d
mailing list