Implicit type conversions

About Monkey 2 Forums Monkey 2 Programming Help Implicit type conversions

This topic contains 3 replies, has 2 voices, and was last updated by  Arjailer 2 years, 11 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1396

    Arjailer
    Participant

    The Implicit type conversions section in the help states that Any numeric type will be automatically converted to Any numeric type.

    However, I’ve got a method which takes doubles, Function AreEqual:Void(expected:Double, actual:Double), and if I try to pass floats in, AreEqual(123.45, 123.45), the compiler compains with “can’t find overload … with argument types (float, float)”.

    The same thing happens with other numeric data types, e.g. declared as Long and passing Int.

    Am I missing something or have I misunderstood?

    #1445

    Mark Sibly
    Keymaster

    This works fine here:

    If you have defined several overloads of ‘AreEqual’ you may have problems if the compiler can’t work out which one you are meaning to call, eg: if you define AreEqual( Double,Double ) and AreEqual( Long,Long ) then you’ll get an error as AreEqual( 123.45,123.45 ) is ambiguous.

    #1508

    Arjailer
    Participant

    Yep – I have a Long overload as you guessed – that’ll be the problem  🙂

    I was hoping that I’d just need Double and Long overloads and then could pass in any of the numeric types – something like how C# does it where it’ll implicity convert to “larger” types, but you need to explicity convert to “smaller” types: https://msdn.microsoft.com/en-us/library/y5b434w4.aspx

    But it’s not really a problem adding overloads for the other numeric types.

    For testing these (and other) functions is there any way to specify a double literal in code? e.g. Java and C# let you use 123.45f for float, 123.45d for double, 123L for long etc.

    #1538

    Arjailer
    Participant

    I’ve sorted it now using generics  🙂

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.