auto limitation?

Ali Çehreli acehreli at yahoo.com
Tue Sep 11 11:33:14 PDT 2012


On 09/11/2012 11:10 AM, Namespace wrote:
 > I have this code, but it works not as expected:
 > http://dpaste.dzfl.pl/6ce5b4dd
 >
 > I get 0 instead of 42 if my type is Int.
 > My value is correct (as you can see) but "writeln" prints still 0
 > instead of 42.
 > I think "auto" compiles first to float and cannot handle then integers.
 > Am I right? And could you explain me how this could work?

Here is a reduced code:

import std.stdio;

enum Type { Int, Float }

auto foo(Type t)
{
     final switch (t) {
     case Type.Int:
         return 42;
     case Type.Float:
         return 1.5;
     }
}

void main()
{
     writeln(foo(Type.Int));
     writeln(foo(Type.Float));
}

The return type of foo() is double. (It's float in your code but it 
doesn't matter.)

I think this is a bug. I guess that 'return 42' is still placing an int 
onto the program stack instead of a float. A workarounds are returning 
to!float(this._num.ivalue).

But I think this is a compiler bug.

Ali


More information about the Digitalmars-d-learn mailing list