Thread overview
'auto' keyword
Mar 12, 2023
DLearner
Mar 12, 2023
Adam D Ruppe
Mar 12, 2023
kdevel
Mar 12, 2023
Salih Dincer
Mar 12, 2023
Paul Backus
Mar 12, 2023
Ali Çehreli
March 12, 2023

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.

  1. 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

March 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:

No, it only actually does #2 in your thing. The type is optional 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.
March 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:

auto arr = [ 1, 2, 3 ];

Moreover, auto ref or ref auto is needed in functions.

SDB@79

March 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".

March 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
March 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