Why do I have to cast arguments from int to byte?

Igor Shirkalin mathsoft at inbox.ru
Tue Oct 10 20:39:16 UTC 2017


On Tuesday, 10 October 2017 at 19:55:36 UTC, Chirs Forest wrote:
> I keep having to make casts like the following and it's really 
> rubbing me the wrong way:
>
> void foo(T)(T bar){...}
>
> byte bar = 9;
>
> foo!byte(bar + 1); //Error: function foo!byte.foo (byte bar) is 
> not callable using argument types (int)	
> foo!byte(cast(byte)(bar + 1));
>
> It wouldn't be so bad if I didn't have to use the word cast 
> before each cast, bust since I have to specify both the word 
> cast and the cast type and then wrap both the cast type and the 
> value in brackets... it just explodes my code into multiple 
> lines of unreadable mess.
>
>
> void foo(T)(T bar, T bar2, T bar3){...}
>
> byte foobar = 12;
>
> foo!byte(foobar + 1, foobar + 22, foobar + 333);
> vs.
> foo!byte(cast(byte)(foobar + 1), cast(byte)(foobar + 22), 
> cast(byte)(foobar + 333));
>
> Why?

Because int (1) + ubyte (9) = int



More information about the Digitalmars-d-learn mailing list