About Monkey 2 › Forums › Monkey 2 Programming Help › any suggestion for importing external generic method
Tagged: extern generic
This topic contains 4 replies, has 2 voices, and was last updated by
Mark Sibly
1 year, 9 months ago.
-
AuthorPosts
-
June 30, 2017 at 3:11 pm #9054
I’d like to finish the box2d modules and for that I only need to import some generic methods (from non generic struct).
The only thing missing then would be operators. I’ve seen that operators are not possible for now but I saw that it was almost ok in the output cpp file (just to say it). And after all, operators are an mx2 feature too.. So it would be great if we could import them (in my test the +cpp just had some postfix member operator and brackets to be removed and it compiled ok).
…But this topic is about external generic methods!
This is a method (Query) that I’d like to be able to call from mx2:
[/crayon]Monkey12345678[crayon-5cba1666985d6772321486 inline="true" ]class b2BroadPhase{public:template <typename T>void Query(T* callback, const b2AABB& aabb) const;..I made simplified test to see how it could be imported. The global function with generics could be imported without problem and the passing by ref if OK! but the method was not…
test.monkey2
[/crayon]Monkey123456789101112131415161718192021222324252627282930313233343536373839[crayon-5cba1666985dd057981161 inline="true" ]Namespace myapp#Import "<std>"#Import "v2.h"Using std..Externstruct sVect2Field x:FloatField y:FloatMethod New()Method New(a:float,b:float)Method aroule:float (a:Float ptr)Method MSwap<T>(a:T,b:T)EndFunction FSwap<T>(a:T,b:T)PublicFunction Main()Local va:sVect2va=New sVect2(1.0,2.0)Local vb:sVect2vb=New sVect2(20.0,20.0)Local arr:=New Float[] (10.0,7.0)Print va.aroule(arr.Data)Print va.x+" "+vb.xFSwap (va,vb) 'this works with variables passed by referencePrint va.x+" "+vb.x 'swap has succeded !!'va.MSwap(va,vb) 'This one doesn't work (has 3 params instead of 2 and g++ says " 'MSwap' was not declared in this scope ")Print va.x+" "+vb.x ' :(Endv2.h
[/crayon]Monkey1234567891011121314151617181920212223242526272829303132333435363738[crayon-5cba1666985e3503829564 inline="true" ]#ifndef V2_H#define V2_Hstruct sVect2{float x, y;//sVect2() = default;//sVect2(float a, float b){x = a;y = b;}//float aroule(float a[2]){return a[0]+a[1];}template<typename T>void MSwap(T& a, T& b){T tmp = a;a = b;b = tmp;}};//template<typename T>void FSwap(T& a, T& b){T tmp = a;a = b;b = tmp;}#endifif I uncomment the line with MSwap I get this error:
[/crayon]Monkey1234[crayon-5cba1666985e9362968457 inline="true" ]error: 'MSwap' was not declared in this scopeMSwap(f0.l_va,f0.l_va,f0.l_vb);any suggestion on how to deal with that T* method?
It’s fun to see that it has 3 parameters instead of 2 and that the additional parameter is actualy the instance that has the scope. So everything seem to be there but not well placed?!
June 30, 2017 at 7:07 pm #9060I just saw in the docs commit of yesterday that extern generic functions can not be imported. Global extern generic functions I tested did work though..
July 2, 2017 at 10:14 pm #9093I am VERY uncomfortable with attempting to support this (and ‘native’ operator overloading) for the simple reason I can’t guarantee it’ll work reliably all of the time without giving ‘weird’ results. In fact, I know it wont.
The main reason for this is that monkey2 doesn’t actually use native c++ generics or operator overloading when translating monkey2 code so they actually don’t work identically to c++’s. There are things you can do with monkey2 operators/generics that you CAN’T do in c++ because of this – and of course vice versa.
So sorry, but the current official line is ‘no native generics/operators’ (even if it’s not fully enforced yet) as I don’t want to have to support c++ generics/operators on top of having to support monkey2 generics/operators, if you see what I mean.
As for box2d, I believe there are existing wrappers around that don’t use generics (objc? bmx? monkeyx?) so it should at least be possible (if not as easy).
July 3, 2017 at 11:05 am #9108Ok thanks for the info. Just wanted it to be as identical as possible to original box2D. Will wrap it around then. Don’t know what to do with generics global function though, because they actualy work, so I think I’ll keep them like that until(if) a problem occurs..
July 3, 2017 at 10:43 pm #9112Don’t know what to do with generics global function though
What do other wrappers do? I would recommend NOT leaving them as is though as I may prohibit extern generics altogether soon to prevent any confusion.
-
AuthorPosts
You must be logged in to reply to this topic.