www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - 'auto' keyword

reply DLearner <bmqazwsx123 gmail.com> writes:
Is it correct that this _single_ keyword is used to indicate 
_two_ quite different things:

1. As a shorthand to make the type of the variable being declared 
the same as the type on the right hand side of an initial 
assignment.

Example: ```auto A = 5;``` makes A an int.

2. To indicate storage class of variable.

Example: ```auto int A;``` (I guess) makes A have automatic 
storage, ie contents lost when control goes out of scope, unlike 
static.

Best regards
Mar 12 2023
next sibling parent reply Adam D Ruppe <destructionator gmail.com> writes:
On Sunday, 12 March 2023 at 13:07:58 UTC, DLearner wrote:
 Is it correct that this _single_ keyword is used to indicate 
 _two_ quite different things:
meaning *any* storage class will work for type inference. `auto` is not special in variable declarations. (it can be special in other contexts though). auto A = 5; static A = 5; const A = 5; all the same.
Mar 12 2023
parent kdevel <kdevel vogtner.de> writes:
On Sunday, 12 March 2023 at 13:27:05 UTC, Adam D Ruppe wrote:
 [...] *any* storage class will work for type inference. [...]
After heaving read [1] I immediately thought of this: void main () { deprecated i = 3; i = 4; } $ dmd test.d test.d(4): Deprecation: variable `test.main.i` is deprecated Does that make sense??? [1] https://issues.dlang.org/show_bug.cgi?id=7432 Issue 7432 - DMD allows variables to be declared as pure
Mar 12 2023
prev sibling next sibling parent reply Salih Dincer <salihdb hotmail.com> writes:
On Sunday, 12 March 2023 at 13:07:58 UTC, DLearner wrote:
 Is it correct that this _single_ keyword is used to indicate 
 _two_ quite different things:

 1. As a shorthand to make the type of the variable being 
 declared the same as the type on the right hand side of an 
 initial assignment.
The auto keyword is really helpful for shortening it. But in at least 2 cases (one of which is interfaces) it should help the compiler. For example, contrary to expected, it is dynamic array: ```d auto arr = [ 1, 2, 3 ]; ``` Moreover, `auto ref` or `ref auto` is needed in functions. SDB 79
Mar 12 2023
parent Paul Backus <snarwin gmail.com> writes:
On Sunday, 12 March 2023 at 15:31:07 UTC, Salih Dincer wrote:
 Moreover, `auto ref` or `ref auto` is needed in functions.
That's because `ref` isn't part of the argument or return value's type, so it isn't covered by **type** inference. Instead, D has a totally separate feature for "`ref` inference".
Mar 12 2023
prev sibling parent =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 3/12/23 06:07, DLearner wrote:

 1. As a shorthand to make the type of the variable being declared the
 same as the type on the right hand side of an initial assignment.
As Adam explained, D already has type inference without a special keyword. However, some places where 'auto' (or 'const', etc.) appear is not only for "shorthand" but for necessity. Some types cannot be spelled-out at all: auto foo() { struct S {} return S(); } void main() { pragma(msg, typeof(foo())); auto s = foo(); } The name 'S' is available only inside 'foo', so code outside has no choice but to use 'auto' (or 'const', etc.) Having said that, it is still possible to alias the returned type, which may be cumbersome in some cases because you may have to come up with a clean expression for attempting to call the function. In this case it's trivial because foo does not take any parameter: alias T = typeof(foo()); T t; // <-- There: I did not need to use 'auto' Ali
Mar 12 2023