About Monkey 2 › Forums › Monkey 2 Development › Generic function types?
This topic contains 2 replies, has 2 voices, and was last updated by
Arjailer
2 years, 8 months ago.
-
AuthorPosts
-
August 8, 2016 at 1:52 pm #2859
Is it possible to have generic function types? For example I’d like to do this:
Monkey12345678910111213141516171819202122Method TestGenericClass:Void()RepeatTestForEachType(Lambda<T>:Void()Local instance := New T()' test an aspect of the classEnd)RepeatTestForEachType(Lambda<T>:Void()Local instance := New T()' test a different aspect of the class without having to repeat the code for each typeEnd)' etc.EndMethod RepeatTestForEachType:Void(test<T>:Void())test<GenericClass<Double>>()test<GenericClass<Float>>()test<GenericClass<Long>>()test<GenericClass<Int>>()EndSo that I don’t have to repeat as much boilerplate code for each test I write. This is something I do all the time in my C# day job and it’d be great to be able to use the same patterns
It doesn’t seem to be possible, but I might just be getting the syntax wrong.
August 8, 2016 at 9:40 pm #2886Not sure what you’re trying to do there – why is TestGenericClass a method? etc…
Generic methods/functions are supported though, eg:
Monkey1234Function Min<T>:T( x:T,y:T )Return x<y ? x Else yEndYou need the ‘<T>’ after the ‘Min’ so the compiler knows that ‘x:T’ and ‘y:T’ refer to generic types, not just a type ‘T’ that might be declared elsewhere.
August 8, 2016 at 10:00 pm #2889Hmmm … I’m getting myself confused …
I was writing tests for a generic class and wanted to test it with various types. To do this I was having to write two methods for each test, sort of like:
Monkey12345678910Method TestSomething:Void()DoSomething<GenericClass<Double>>()DoSomething<GenericClass<Long>>()etc.EndMethod DoSomething<T>:Void()Local instance := New T()' test an aspect of the classEndThis was getting a bit tedious, so I was looking for a way to cut this down a bit.
But thinking about it again there’s two obvious problems with that:
- What I was trying to do was nonsense (I somehow convinced myself that what I was trying to do was possible in C#, but it’s not – total brain-fart)
- I don’t need to test it with different types – one is enough (another brain-fart)
So, sorry to waste your time
-
AuthorPosts
You must be logged in to reply to this topic.