auto limitation?

Graham Fawcett fawcett at uwindsor.ca
Tue Sep 11 17:59:17 PDT 2012


On Tuesday, 11 September 2012 at 21:31:17 UTC, Namespace wrote:
> On Tuesday, 11 September 2012 at 21:13:02 UTC, bearophile wrote:
>> Namespace:
>>
>>> I have this code, but it works not as expected:
>>> http://dpaste.dzfl.pl/6ce5b4dd
>>
>> I suggest to file a bug:
>>
>>
>> auto foo(bool b) {
>>    final switch (b) {
>>        case true:
>>            return 10;
>>        case false:
>>            return 20.0;
>>    }
>> }
>> void main() {
>>    import std.stdio: writeln;
>>    writeln(foo(true));
>>    writeln(foo(false));
>> }
>>
>>
>> The acceptable results are a compile-time error for type 
>> mismatch, or a conversion of both literals to double. Probably 
>> the second is better. But a silent bit-level cast is not 
>> acceptable.
>>
>> Bye,
>> bearophile
>
> Sure that is the first i will do tomorrow.
> But so far no suggestions?

Just this one. You can use an anonymous union in your Num struct, 
so you can write "obj.ivalue" rather than obj._num.ivalue":

struct Num {
private:

   final enum Type {
     None,
     Float,
     Int
   }

   union {
     float fvalue;
     int ivalue;
   }

   Type _type;

   ....

Graham



More information about the Digitalmars-d-learn mailing list