Idea : Expression Type

Don Clugston dac at nospam.com.au
Fri Feb 2 12:47:22 PST 2007


Andrei Alexandrescu (See Website For Email) wrote:
> Xinok wrote:
>> Functions aren't powerful enough to be used in place of expressions.
>>
>> -- Functions can't return references, which prevents you from using 
>> them as l-values. The best you can do is return a pointer, which you 
>> have to manually dereference.
>> -- All of the types in an expression are automatic, including the 
>> return type. Getting the parameter types for a function can be simple, 
>> but the return type can be difficult.
>> -- Functions can't take full advantage of features like conditionals, 
>> because conditionals can have two or more possible 'return types'. 
>> Functions can only return a single type.
> 
> Conditionals always have one return type that can be detected as 
> typeof(true? T : U).
> 
> The advantage of expression aliases is that indeed there's no need to 
> worry for their type. 

That's already possible, albeit clumsy. You can duplicate the expression 
inside an anonymous delegate, and use D's automatic return type 
inference, together with an is( : return) expression. This works even 
when there are statements in the delegate, together with expressions.

So I suspect something even more general might be possible.

Also, expression aliases work when passing e.g.
> constant expressions to templates because they expand during 
> compilation. The disadvantages are that (1) writing statements in the 
> replacement expression is not possible, (2) separate compilation is not 
> possible.
> 
> 
> Andrei



More information about the Digitalmars-d mailing list