Unexpectedly nice case of auto return type

Basile B. b2.temp at gmx.com
Tue Dec 3 07:12:18 UTC 2019


I wish something like this was possible, until I change the 
return type of `alwaysReturnNull` from `void*` to `auto`.


---
class A {}
class B {}

auto alwaysReturnNull() // void*, don't compile
{
     writeln();
     return null;
}

A testA()
{
     return alwaysReturnNull();
}

B testB()
{
     return alwaysReturnNull();
}

void main()
{
     assert( testA() is null );
     assert( testB() is null );
}
---

OMG, isn't it nice that this works ?

I think that this illustrates an non intuitive behavior of auto 
return types.
One would rather expect auto to work depending on the inner 
return type.


More information about the Digitalmars-d-learn mailing list