How about "auto" parameters?

Andrei Alexandrescu SeeWebsiteForEmail at erdani.org
Tue Jun 7 07:57:30 PDT 2011


On 6/7/11 7:11 AM, foobar wrote:
> I agree with Ary above and would also like to add that in the ML family of languages all the variables are also default auto typed:
> E.g.:
> fun add a b = a + b
>
> 'add' would have the type ('a, 'a) ->  'a and the type inference engine will also infer that 'a must provide the + operator.
> I feel that this is more natural than having a dedicated function template syntax.

I agree it would be nice to further simplify generic function syntax. 
One problem with the example above is that the type deduction didn't go 
all that well - it forces both parameter types to be the same so it 
won't work with adding values of different types (different widths, 
mixed floating point and integrals, user-defined +). In a language 
without overloading, like ML, things are a fair amount easier.

> Better yet, instead of auto parameters, just make parameter types optional (as in ML) and let the compiler generate the template.
>
> foo(a, b) { return a + b; } // will be lowered to:
> T foo(T) (T a, T b) { return a + b; }
>
> Types are already inferred for delegate literals so why not extend this to regular functions too?

There are multiple issues. One is we don't have Hindley-Milner 
polymorphism. The D compiler doesn't really "infer" types as "propagate" 
them. Another is, such inference would make separate compilation difficult.


Andrei


More information about the Digitalmars-d mailing list