Class' Where conditions

About Monkey 2 Forums Monkey 2 Programming Help Class' Where conditions

This topic contains 4 replies, has 2 voices, and was last updated by  nerobot 1 year, 5 months ago.

Viewing 5 posts - 1 through 5 (of 5 total)
  • Author
    Posts
  • #11695

    abakobo
    Participant

    What would be the syntax for the new where for class conditions? And what is the purpose of this feature?

    Where conditions for classes. Class declaration can now include a ‘where condition’ at the end of the regular class declaration. If the where condition evaluates to false when the class is instantiated, a compile time error occurs.

    In this example I get no compilation error. But I supposed ‘Where False’ would induce the compilation error..

    #11743

    abakobo
    Participant

    And I don’t get how a compilation error can occur while the class is instantiated, wouldn’t it be a runtime error?

    #11745

    nerobot
    Participant

    What would be the syntax for the new where for class conditions? And what is the purpose of this feature?

    Don’t use it just because you can if you don’t really need that. 🙂

    And I don’t get how a compilation error can occur while the class is instantiated, wouldn’t it be a runtime error?

    Not a runtime error but compile time error!

    mx2cc checks variables types at compile time.

    See my example below. There is an compilation error. Just comment where condition and you can compile it.

    Both classes have DoSomething methods => we can use them as generic type.

    But if we want to use ComponentHolder with components only – we add Where condition to deny other types.

    #11746

    abakobo
    Participant

    Ok I though this was not new and that the new feature announced in the blog post was not for generics.

    I still don’t get when the class has been instantiated if the program could not run. Afaik you have to call New() to get an instance. Or Mark meant instantiated in the compiler logic?

    Anyway, nevermind, if it’s a generics thing I know the purpose of it 😉

    thanks

    #11761

    nerobot
    Participant

    Ok I though this was not new and that the new feature announced in the blog post was not for generics.

    New part of Where is abilitity to combine conditions by And / Or / Not operators.

    You can write Where T Extends Component Or T=MyUpdater. Yes, we also can use = and <> in conditions.

    Combined condition is needed also for multi-generic classes, for example:

     

    I still don’t get when the class has been instantiated if the program could not run. Afaik you have to call New() to get an instance. Or Mark meant instantiated in the compiler logic?

    Here: “instantiated” means “code part where we call New()”. Monkey2’s compiler check all New() calls to check are they correct accordingly with Where. C++ compiler do its own checks but doesn’t allow us to deny using of unwanted types as a generics (allow to deny, sounds good:) ).

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

You must be logged in to reply to this topic.