Auto returntype
Alex
AJ at gmail.com
Mon May 20 02:32:47 UTC 2019
On Monday, 20 May 2019 at 00:31:48 UTC, sarn wrote:
> On Sunday, 19 May 2019 at 23:10:08 UTC, Para wrote:
>> Unless I have very poor reading comprehension, which I might,
>> I believe this is already a feature:
>> https://dlang.org/spec/function.html#auto-functions
>
> The type of an auto function is inferred from the return
> argument, but I think Alex wants more sophisticated inference
> based on usage. Maybe something like Hindley-Milner, as used
> in Haskell:
> http://www.codecommit.com/blog/scala/what-is-hindley-milner-and-why-is-it-cool
>
Yes, but it's much easier than that. No reason to infer anything.
The type is the lvalue's type. but it requires one to determine
that the lvalue has a type. In fact, maybe it could be a more
general thing where one could simply get the lvalue(see below).
> D's templates could theoretically support more inference, but
> that would need
> 1) Someone interested enough to implement it
> 2) Someone who can convince Walter that it won't make D's
> template system too complicated
First it should be proved to be a sound idea, else it is
pointless to do 1 and 2.
----
One could in general get the assignment type and use that in code
to perform polymorphic behavior:
T foo()
{
writeln(T.stringof);
T x;
return x;
}
int x = foo();
float x = foo();
prints
int
float
It requires that the lvalue's type be known and parsed before the
template call... not a hard problem but might not be implemented
in D as such.
More information about the Digitalmars-d
mailing list