Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can not the Compiler decide the type

I am just curious to know why do we need to append m or M for decimal type?

Documentation says there is no implicit conversion

I think the compiler has enough information because we declare Decimal key word.

Can some one please explain why can not the compiler determine the value should be treated as decimal but not double.

like image 809
Sandeep Avatar asked Jun 28 '11 02:06

Sandeep


1 Answers

If you have a statement like this

decimal x = 5.6;

the compiler first looks only at the right-hand side of the assignment (5.6), determines its type (double) and then checks if the result can be assigned to the variable on the left-hand side (x).

Since x is declared as decimal but the compiler has determined that the expression is of type double (and there is no implicit conversion from double to decimal), the assignment is invalid.

(The only exception to this rule is assigning a lambda expression to a delegate variable. In this case, the compiler indeed uses the information at the left-hand side to determine the type of the right-hand side.)

like image 161
dtb Avatar answered Nov 07 '22 16:11

dtb