Automatic variable declaration

Everlast Everlast at For.Ever
Wed Aug 15 16:56:39 UTC 2018


On Wednesday, 15 August 2018 at 15:31:14 UTC, 0xEAB wrote:
> On Wednesday, 15 August 2018 at 14:58:40 UTC, Everlast wrote:
>> foo(x = 3);
>
>
> C# 7 brought something similar for out params.
>> int.tryParse("1234", out int myNum);
>
> Based on this approach, I would argue, that a reasonable syntax 
> would also contain the type, which leads to something like the 
> following:
>> foo(int x = 3);
>> foo(auto x = 3);
>
>
> But I don't think this feature is worth having in D.

The whole point of the syntax is just sugar... although it does 
reduce dependency issues ever so slightly. If one has to declare 
the type it reduces it's effectiveness. Of course, if it is 
necessary for some reason then it can be used. It obviously helps 
reduce ambiguities but since D can infer the type from the 
function then it shouldn't be necessary(but could be optional). 
Basically auto could be implied when not explicit. This is a 
little different than an isolated definition because D can infer 
the type here in most cases(maybe templates would cause grief 
though).

The point one really has to ask is what problems does it solve 
and what problems does it create.

The problems it solves can be reduced typing and better 
localization. It is minor, not a huge advancement, but the path 
to heaven is paved with small stones.

The problems it creates: I can't think of any that do not already 
steam from the base problem. If D has assignment syntax that can 
be used inside function arguments then it would be ambiguous and 
in that case the type would need to be required to solve that 
problem.




More information about the Digitalmars-d mailing list