How about "auto" parameters?

foobar foo at bar.com
Tue Jun 7 09:01:05 PDT 2011


Andrei Alexandrescu Wrote:

> 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.
> 

ML is strictly typed unlike C-like languages. This is a *good* thing and is a feature. While C's implicit casts are a horrible hole in the language. Also ML has only two types: integers and floating point. There is no short vs long problems. 
Yes, both arguments will have the same type but this is the correct default. When adding a floating point and an integral the user should be required to specify what kind of operation is being made, either the double is converted to an integral (how? floor, round, etc? ) or the integral is converted to a floating point which can cause a loss of precision. Although overloading complicates things it doesn't mean it's impossible. 

ML is explicit but only in the correct places. C-like languages have shortcuts but those are in the wrong places where it hurts and it's verbose in other places. I prefer to let the compiler infer types for me but require me to be explicit about coercion which is type safe vs. the reverse which is both more verbose and less safe. 

> > 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

We don't Hindley-Miler _yet_. I can hope, can't I? 
Again, difficult != impossible. AFAIK it is working in Nemerle, isn't it? 




More information about the Digitalmars-d mailing list