Auto returntype

Alex AJ at gmail.com
Sun May 19 22:57:50 UTC 2019


One thing D does not have in it's type system is the ability to 
infer the return type from the usage:

O[] foo() { O[] x; return x; }
byte[] q = foo();

The idea is that O is determined to be byte. The compiler looks 
at the assignment and realizes that we want foo to return a byte 
array and that O then must be a byte.

This avoids having to specify the type in foo
e.g., we could have

auto foo(O)() { O[] x; return x; }
auto q = foo!byte();


I think the first case makes more sense. They are effectively the 
same, at least in initialization code when the lvalue's type can 
be determine.

What it mainly does is avoid having to specify the return type as 
a parameter and looks more natural.
If there were never an issues this would be idea because it can 
save a parameter in some cases and make for cleaner code.


If no one see's any issues with this then it would be a simple 
rewrite rule.

Can anyone see any failures of such code?

[Can, the return type much be determined from the lvalue]



More information about the Digitalmars-d mailing list