About Monkey 2 › Forums › Monkey 2 Programming Help › Where keyword
Tagged: generics, where
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)
-
AuthorPosts
-
February 20, 2019 at 2:52 am #16072
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
February 20, 2019 at 2:58 am #16073ParticipantOK 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
February 20, 2019 at 8:20 am #16074Welcome back! I would never have figured that out, so that’s handy to know… thanks!
February 20, 2019 at 11:24 am #16075Thanks, 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
February 24, 2019 at 10:20 am #16085[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?
February 24, 2019 at 2:20 pm #16086Yes, 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.February 24, 2019 at 4:53 pm #16088Cool.
-
AuthorPosts
You must be logged in to reply to this topic.