"else if" for template constraints

Idan Arye via Digitalmars-d digitalmars-d at puremagic.com
Mon Aug 17 10:00:30 PDT 2015


On Monday, 17 August 2015 at 13:18:43 UTC, Steven Schveighoffer 
wrote:
> void replaceInPlace(T, Range)(ref T[] array, size_t from, 
> size_t to, Range stuff)
> if(isDynamicArray!Range &&
>     is(Unqual!(ElementEncodingType!Range) == T) &&
>     !is(T == const T) &&
>     !is(T == immutable T))
> { /* version 1 that tries to write into the array directly */ }
>
> void replaceInPlace(T, Range)(ref T[] array, size_t from, 
> size_t to,
> Range stuff)
> else if(is(typeof(replace(array, from, to, stuff))))
> { /* version 2, which simply forwards to replace */ }

It looks a bit ugly, that the `else` is after a function 
declaration instead of directly after the if's "then" clause. How 
about doing it with the full template style?

     template replaceInPlace(T, Range)
     if(isDynamicArray!Range &&
         is(Unqual!(ElementEncodingType!Range) == T) &&
         !is(T == const T) &&
         !is(T == immutable T))
     {
         void replaceInPlace(ref T[] array, size_t from, size_t 
to, Range stuff)
         { /* version 1 that tries to write into the array 
directly */ }
     }
     else if(is(typeof(replace(array, from, to, stuff))))
     {
         void replaceInPlace(ref T[] array, size_t from, size_t 
to, Range stuff)
         { /* version 2, which simply forwards to replace */ }
     }


More information about the Digitalmars-d mailing list