The D Scripting Language

Per Ångström d-news at autark.se
Mon Nov 15 10:53:31 PST 2010


On 2010-11-15 18:40, Simen kjaeraas wrote:
> Per Ångström <d-news at autark.se> wrote:
>
>> /++
>> Simulates type-returning or-expression
>> +/
>> template or(T) {
>> T _(T a, lazy T b) {T tmp = a; return tmp ? tmp : b;}
>> }
>
> You should probably use a function template[1] or at least an eponymous
> template here:
>
> // function template:
> auto or( T )( T a, lazy T b ) {
> return a ? a : b;
> }
>
> // eponymous template:
> template or( T ) {
> auto or( T a, lazy T b ) {
> return a ? a : b;
> }
> }
>
> [1]: http://digitalmars.com/d/2.0/template.html#function-templates

Great, using either of those makes this possible:

     string s = or(func2("..."), "default");

Which is a lot less ugly.

Cheers,
-- 
Per Å.


More information about the Digitalmars-d mailing list