Automatic variable declaration
Everlast
Everlast at For.Ever
Wed Aug 15 14:58:40 UTC 2018
Many times one must create a variable before a function call:
int x = 3;
foo(x);
writeln(x);
and this is fraught with problems such as dependencies(move or
change the second line and one has to validate how the first line
is affected along with all the others).
A new an improved technique, which consolidates the two lines in
to one is to automatically have the compiler define the variable:
foo(x = 3);
writeln(x); // x was implicitly created in this scope by foo.
The the type of x is inferred from the type of the parameter for
foo and it is initialized to 3 before calling foo(which may be
optimized away, say, if the compiler can deduce that the initial
state of x is not needed).
foo(x = 3);
is semantically equivalent to
int x = 3;
foo(x);
by requiring an initialized value using the equal size,
disambiguates instantiation and prevents instantiation of
mistyped variables which would not give a compile time error.
With init, defaults can be used.
The problems for both cases are identical as far as one only
gains a bit better localization.
The notation may seem a little funky but it is perfectly valid
since this is as simple rewrite rule(direct isomorphic
correspondence law).
More information about the Digitalmars-d
mailing list