Where keyword

About Monkey 2 Forums Monkey 2 Programming Help Where keyword

Tagged: ,

This topic contains 6 replies, has 4 voices, and was last updated by  Amon 1 week, 4 days ago.

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

  • Author
    Posts
  • #16072

    NoOdle

    Participant

    Just started playing around with Monkey2, decided it was time to move on from Monkey1. Very excited with some of the language improvements/additions. While exploring the help files I came across the Where keyword but I’ve been struggling to work out what it actually does and how to use it.

    Any help would be appreciated!

    Thanks

    #16073


    NoOdle

    Participant

    OK after some more tinkering I have figured it out

    Struct Test<T> Where T = Int Or T = Float

    Constrains generics like it says in the help file, pretty handy for things like Vec2

    #16074

    DruggedBunny

    Participant

    Welcome back! I would never have figured that out, so that’s handy to know… thanks!

    #16075

    NoOdle

    Participant

    Thanks, its good to be back!

    I don’t think I would have had the patience to figure it out if it wasn’t for those lovely red squiggly lines, allowed me to try lots of different combinations until it worked… although I discovered later on with variants that no squiggles doesn’t mean it will actually run

    #16085

    Amon

    Participant

    [quote]

    OK after some more tinkering I have figured it out

    Struct Test<T> Where T = Int Or T = Float

    Constrains generics like it says in the help file, pretty handy for things like Vec2

    [/quote]

    Am I right then in thinking that with this implementation Test will only accept ints and floats, no strings, bools etc?

    #16086

    abakobo

    Participant

    Yes, the compiler should tell you that you are using a non accepted type.
    If I remember well you can also use ‘implements’ and ‘extends’
    Where <T> implements myInterface
    Where <T> extends myBaseClass.

    #16088

    Amon

    Participant

    Cool.

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

You must be logged in to reply to this topic.