More D newb questions.

BCS BCS at pathlink.com
Fri May 9 10:26:29 PDT 2008


Me Here wrote:
> BCS wrote:
> 
> 
>>void Fn(T)()
>>{
>> T a, b;
>> auto c = a ~ b;
>> T d = c;
>>}
> 
> 
> Okay. What do you think happens when you try to compile this?
> 
> import std.stdio;
> 
> T Fn(T)( T a, T b ) {
>     auto c = a ~ b;
>     T d = c;
>     return d;
> }
> 
> int main( char[][] args ) {
>     char[][] a = [ [ 'a', 'b' ], [ 'c', 'd' ] ];
>     char[][] b = [ [ 'p', 'q' ], [ 'r', 's' ] ];
>     
>     writefln( Fn!( char[][] )( a, b ) );
>     return 1;
> }
> 
> 
> 

it works:  char[][] ~ char[][] -> char[][]


but (still using your rules) this fails:
   writefln( Fn!( char )( 'a', 'b' ) );
because: char ~ char -> char[] != char


so to make the function work you need:

typeof(T~T) Fn(T)( T a, T b ) {
     auto c = a ~ b;
     T d = c;
     return d;
}

or:

T Fn(T)( T a, T b ) {
     static assert(is(T == typeof(a~b),
         "Fn can only take arrays not "~T.stringof);

     auto c = a ~ b;
     T d = c;
     return d;
}



More information about the Digitalmars-d mailing list