digitalmars.D.learn - 'auto' keyword
- DLearner (11/11) Mar 12 2023 Is it correct that this _single_ keyword is used to indicate
- Adam D Ruppe (9/11) Mar 12 2023 No, it only actually does #2 in your thing. The type is optional
- kdevel (12/13) Mar 12 2023 After heaving read [1] I immediately thought of this:
- Salih Dincer (9/14) Mar 12 2023 The auto keyword is really helpful for shortening it. But in at
- Paul Backus (4/5) Mar 12 2023 That's because `ref` isn't part of the argument or return value's
- =?UTF-8?Q?Ali_=c3=87ehreli?= (22/24) Mar 12 2023 As Adam explained, D already has type inference without a special keywor...
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
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
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
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
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
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