Dne 30.5.2017 v 23:16 Oleg B via Digitalmars-d-learn napsal(a):
Hello. I have this code

import std.stdio;

void foo(byte a) { writeln(typeof(a).stringof); }
void foo(short a) { writeln(typeof(a).stringof); }
void foo(int a) { writeln(typeof(a).stringof); }

void main()
{
    foo(0); // int, and byte if not define foo(int)
    foo(ushort(0)); // byte (unexpected for me)
    foo(cast(ushort)0); // byte (unexpected too)

    foo(cast(short)0); // short
    foo(short(0)); // short

    ushort x = 0;
    foo(x); // short
}

Is this a bug or I don't understand something?

It is "not" a bug, and it is how compiler works. Compiler do many assumptions (it is sometimes useful). So if it see something like immutable ushort y = 0 (compile time value) or just "0" (still compile time value) it knows its value so it knows it can be saftly represent in byte, ubyte and so on.

However ushort x = 0;
foo(x); // here x is runtime value so compilere does not know the size, so it will use the type.

Lets look to an another example

immutable ushort y = 0;
byte x = y;

this will compile because of current rules.

but this:

ushort y = 0;
byte x = y;

will produce this error: Error: cannot implicitly convert expression (y) of type ushort to byte

And in this example is everything ok:

import std.stdio;

void f(ushort u)
{
    writeln("ushort");
}

void f(ubyte u)
{
    writeln("ubyte");
}

void main()
{
    ushort y = 0;
    immutable ushort x = 0;

    f(y);
    f(x);
}

RESULT IS:
ushort
ushort

but obviously in follow example it could be misleading

import std.stdio;

void f(short u)
{
    writeln("short");
}

void f(ubyte u) // or byte u
{
    writeln("ubyte or byte");
}

void main()
{
    ushort y = 0;
    immutable ushort x = 0;

    f(y);
    f(x);
}

RESULT IS:
ushort
[u]byte

But it make sense, compiler will select the best match in this case it is byte or ubyte but if we change x to something like 128
wi will get different results for byte and ubyte


Reply via email to