Can't use Color in Select/Case statement

Home Forums Monkey 2 Development Can't use Color in Select/Case statement

This topic contains 6 replies, has 3 voices, and was last updated by  Mark Sibly 1 week, 3 days ago.

Viewing 7 posts - 1 through 7 (of 7 total)
  • Author
    Posts
  • #10344

    TomToad
    Participant

    Don’t know if this is a bug, or I just misunderstand how Select/Case works.
    If I compare 2 Color objects with If, everything works.

    However, when making a comparison within a Select/Case, I get an error

    #10346

    nerobot
    Participant

    Because r/g/b/a components of Color is Float type, and Float is not comparable type.

    Try to run these examples:

    #10348

    TomToad
    Participant

    Your example has nothing to do with my problem.  [b]If currentColor = Color.White [/b] works just fine.  Succeeds when currentColor is white, fails on any other color.  The problem is not with If/Then, but with Select/Case, which won’t even compile.  Your example compiles fine, even though you don’t get the expected results.

    Also, floats are comparable.  The reason why your test fails is due to the tiny imprecision caused by storing floats.  Due to that imprecision, they are rarely exactly equal. Instead, you should subtract the two and compare against a tolerance.  If c-(a-b) < .001 gives the expected results in your example.

    #10349

    nerobot
    Participant

    Hmm. If we add operator <=> via extension, we get different error message

    Probably, something wrong in generated c++ code.

    Other operators <> ,< , > , = don’t solve error.

    #10350

    nerobot
    Participant

    And the same problem have a place if we create other struct like color with comparison operators.

    #10352

    TomToad
    Participant

    I see the problem now.  When the std.graphics.color module is compiled, an overloaded bbCompare() function is created.  In this function, the fields are compared 1×1 and result is returned.  This is what the If/Else/End statement is using to compare the two parameters.  If I overload the = operator, a function called m_eq() is created and this is used instead of bbCompare.

    Monkey2

    Compiles to C++

    Now the Select/Case statement is different.  Instead of using bbCompare(), it is using C++’s built in == operator.  Since Monkey2 has no way to overload C++’s == operator, it creates the error.

    monkey2

    Compiles to C++

    So the solution would be either 1) Have Select/Case use bbCompare instead of == or 2) be able to override == at the C++ level.

    #10359

    Mark Sibly
    Keymaster

    It’s definitely a compiler bug, would you mind posting an issue at github?

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

You must be logged in to reply to this topic.