auto limitation?

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


On Tuesday, September 11, 2012 23:31:43 Namespace wrote:
> Sure that is the first i will do tomorrow.
> But so far no suggestions?

Use a cast. auto _cannot_ be different types depending on what you return. It 
must always resolve to the same type. So, in this case, either it needs to 
resolve to double (in which case the compiler should case) or fail to compile.

If you really want the type of the return type to be different depending on 
what you return, you need to use Variant (in which case it _still_ won't 
really be different, it'll just always be Variant, but Variant will hold a 
different value in its internal union, and you'll be able to get that value out 
as its original type later, because the Variant knows what it was). But D is 
statically typed, so all types must be known at compile time, and you can't 
have types changing based on the path of execution.

- Jonathan M Davis


More information about the Digitalmars-d-learn mailing list