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

Steven Schveighoffer schveiguy at yahoo.com
Fri Oct 13 12:13:35 UTC 2017


On 10/13/17 3:47 AM, Daniel Kozak wrote:
> but it works ok with immutable, so until you really need to change bar 
> you can use
> 
> immutable bar = 9;
> foo!byte(bar + 1);

Right, the reason why your original didn't work is the compiler 
"forgets" that bar is 9 by the time it gets to the foo call. However, it 
doesn't forget the value of an immutable.

> 
> On Fri, Oct 13, 2017 at 9:46 AM, Daniel Kozak <kozzi11 at gmail.com 
> <mailto:kozzi11 at gmail.com>> wrote:
> 
>     Not sure :), I have forgoten byte+byte=int.

One place where the compiler doesn't care is +=:

bar += 400; // OK
int y = 400;
bar += y; // OK

-Steve


More information about the Digitalmars-d-learn mailing list