Auto function without return statement, prefer an explicit void

bauss jj_1337 at live.dk
Thu May 19 11:09:13 UTC 2022


On Thursday, 19 May 2022 at 09:24:21 UTC, user1234 wrote:
> On Thursday, 19 May 2022 at 09:15:50 UTC, Anonymouse wrote:
>> I like explicit return types. But I also like inference.
>>
>> ```d
>> struct SomeContainer(T)
>> {
>> /* ... */
>>     auto clear()
>>     {
>>         head = 0;
>>         tail = 0;
>>         buf[] = T.init;
>>     }
>> }
>> ```
>>
>> dscanner -S: `Auto function without return statement, prefer 
>> an explicit void`
>>
>> Can we please have `void clear() auto`?
>
> The problem is when in the body you have an `asm` block that 
> returns.
> In that case the return type cant be inferred.
>
> ```d
> module runnable ;
>
> auto notVoid()
> {
>     asm
>     {
>         naked;
>         mov AL, 1;
>         ret;
>     }
> }
>
> void main()
> {
>     auto b = notVoid();
> }
> ```
>
>> Error: variable `runnable.main.b` type `void` is inferred from 
>> initializer `notVoid()`, and variables cannot be of type `void`
>
> but `notVoid()` actually returns a `ubyte`

Surely we could distinguish an auto function with a return 
statement / asm that has ret and an auto function that doesn't 
have any return statements at all.


More information about the Digitalmars-d mailing list