Yearly "can we please have void foo() auto" post
Steven Schveighoffer
schveiguy at gmail.com
Thu Jul 27 22:08:27 UTC 2023
On 7/27/23 2:55 PM, Paul Backus wrote:
> On Thursday, 27 July 2023 at 15:22:45 UTC, Anonymouse wrote:
>> I love `auto`, it makes my life easier. The compiler infers what it
>> can and I primarily enjoy the bonuses of `@safe` without having to
>> annotate it manually.
>>
>> But it's not always applicable.
> [...]
>> Can we please have `void foo() auto`? It would behave as `auto foo()`
>> does, except enforce the return type. As a bonus we'd also get `string
>> bar() auto`.
>
> IMO the correct solution is to just make the compiler infer attributes
> like `@safe` for *all* functions, with exceptions only in cases like
> `extern (C)` or virtual class/interface methods where the full
> implementation is not available, or if the user explicitly opts out
> (e.g., with something like `@noinfer`).
>
> I have a half-written draft DIP for this that I plan to get back to once
> Mike Parker announces that the DIP queue is open again. If anyone else
> is working on this or wants to collaborate, please let me know!
I think it would be nicer to specify exact attributes than to "opt out"
of inference.
For e.g. `@safe`, we are covered, because there is `@system`. For
`nothrow`, there is `throw` (I think?). But for `@nogc` and `pure`,
there is no opposite.
I would like to see some mechanism or additional attributes that make it
so you can specify what should not be inferred before turning on full
inference.
One large problem with all this is people who "don't care" about
attributes. Of course, now their code will infer attributes! But that
has become part of the public API. If they change something to change
the attribute inference, all of a sudden code that is marked explicitly
will stop compiling, and the author will say "too bad, not my fault".
I think the only way to fix this is to only infer when explicitly stated.
And to that effect... we could just make the storage class `auto` mean
that, like the OP says.
I mean, we already allow `static void foo()`, why not `auto void foo()`?
If I do `auto void foo() {}` it complains:
```
Error: function `onlineapp.foo` storage class `auto` has no effect if
return type is not inferred
```
Well, let's make it... have an effect.
-Steve
More information about the Digitalmars-d
mailing list