auto limitation?

Jonathan M Davis jmdavisProg at gmx.com
Tue Sep 11 15:14:43 PDT 2012


On Tuesday, September 11, 2012 23:55:40 bearophile wrote:
> Namespace:
> > But so far no suggestions?
> 
> This seems to work, but solutions that use cast() are sometimes
> fragile (and dangerous):
> 
> 
> auto foo(bool b) {
>      final switch (b) {
>          case false:
>              return 20.0;
>          case true:
>              return cast(typeof(return))10;
>      }
> }
> void main() {
>      import std.stdio: writeln;
>      writeln(foo(false));
>      writeln(foo(true));
> }

If you're going to use a cast, then use one where you give the type 
explicitly. Using typeof(return) is just asking for it when the compiler is 
clearly already having issues with the return type.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list