Monkey accepts float passed into int w/o a cast
Monkey Forums/Monkey Programming/Monkey accepts float passed into int w/o a cast
| ||
Output... The result is 3 The result is 2.3 I understand why the Add:Int function is called for the first line of output; an overloaded function with a default argument is used when no matching overloaded function is found. Just wondering if a float should be passed into int type argument w/out a cast. Strict Global dec_glob:Float = 0.8 Function Add:Int(valOne:Int, valTwo:Int=2) Return valOne + valTwo End Function Add:Float(valOne:Float, valTwo:Float) Return valOne + valTwo End Function Main:Int() local decimal:Float = 1.5 local statement:String = "The result is " Print(statement+Add(decimal)) Print(statement+Add(decimal, dec_glob)) Return 0 End |
| ||
My guess is that Monkey is autoboxing the float to an int. |
| ||
They're primitives so it's just a direct cast in languages where it makes a difference. The "should" of it is not possible to answer, but it's been around for a while so is presumably intended Monkey behaviour. Most languages would give you a warning about loss of precision where an implicit float->int cast is occurring. Monkey doesn't do warnings. |
| ||
I am glad that Monkey does it that way and hope it won't be changed. |
| ||
I think the compiler lets some stuff through that it shouldn't. It's rarely a problem, but it means that sometimes when a bug bites it's not where you expect. However, in the case above, I think Monkey is working as intended. |