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

Reply via email to