About Monkey 2 › Forums › Monkey 2 Programming Help › Possible to add method to Rect with "Extension" ?
Tagged: extension
This topic contains 7 replies, has 6 voices, and was last updated by 
 codifies
 2 years, 5 months ago.
- 
		AuthorPosts
 - 
		
			
				
October 12, 2016 at 8:22 am #4413
Is it possible to add a method to Rect, by the means of the Extention keyword/functionality?
If yes, please post an example, because my attemts results in compiler errors.
October 12, 2016 at 6:39 pm #4425Here you go..
Monkey1234567891011121314151617181920#Import "<std>"Using std..Struct Rect<T> ExtensionMethod Grow:Rect<T>( size:T )Return New Rect<T>( min.x-size,min.y-size,max.x+size,max.y+size )EndEndFunction Main()Local r:=New Recti( 0,0,10,10 )r=r.Grow( 5 )Print rEndActually took me a few attempts to get right – you should be able to use plain ‘Rect’ inside the extension instead of ‘Rect<T>’ (the way you can inside normal class decls) but that’s not working. Will look into it eventually, but Rect<T> will always work.
October 12, 2016 at 7:30 pm #4426OK. Why would you want to use Extension instead of Extends? Can someone explain the rationale behind it?
October 12, 2016 at 8:29 pm #4427OK. Why would you want to use Extension instead of Extends?
The main advantage of extensions is that they can be used with objects that you *didn’t* create. With inheritance, you need to be in control of the ‘new’ that creates the object.
So the above ‘Grow’ extension can be used with any Rect, not just ‘MyRects’ or some other class that Extends Rect. (also, you can’t currently extend structs anyway but that’s not really the point here!).
You can do all sorts of cool things with extensions, like add a DrawMysprite() method to canvas, To:String() or To:JsonValue() to *anything* etc.
There can also be multiple extensions of the same class, as opposed to single inheritance where you can create a new CoolRect1 or a CoolRect2 – but not both.
It’s important to note that extension methods are really just global functions (which is why I was a bit wary of adding them in the first place). But in mx2, some functionality can only be achieved via methods, eg: operators, To:String() etc, so they provide quite a nice way of dealing with that.
October 17, 2016 at 5:45 pm #4475Great, thanks for showing how
November 18, 2016 at 9:47 am #5197Hi,
I need to extend Vec2<T> struct with additionnal features but i have an errors with ‘Property’. Can you tell me if my code is correct and why he doesn’t work?
Monkey1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374#Import "<std>"Using std..Struct Vec2<T> Extension'// By PropertyProperty Angle:Float()Return ATan2(Self.Y, Self.X)Setter:Void(value:Float)Self.X = Cos(value) * Self.LengthSelf.Y = Sin(value) * Self.LengthEndProperty Len:Double() 'Length extensionReturn LengthSetter(value:Double)Local a:Double = ATan2(Self.Y, Self.X)Self.X = Cos(a) * valueSelf.Y = Sin(a) * valueEnd'// By MethodMethod Angle2:Float() 'TestReturn ATan2(Self.Y, Self.X)EndMethod Angle2:Vec2<T>(value:Float) 'TestSelf.X = Cos(value) * Self.LengthSelf.Y = Sin(value) * Self.LengthReturn SelfEndMethod Clone:Vec2<T>()Return New Vec2<T>(Self.X, Self.Y)EndMethod One:Vec2<T>()Self.X = 1.0Self.Y = 1.0Return SelfEndMethod Zero:Vec2<T>()Self.X = 0.0Self.Y = 0.0Return SelfEndMethod Limit:Vec2<T>(value:Double)Local i:T = value / ((Self.Length <> 0.0) ? Self.Length Else 1.0)i = (i < 1.0) ? i Else 1.0Self.X = Self.X * iSelf.Y = Self.Y * iReturn SelfEndEndFunction Main()Local v:=New Vec2f(1,2)Print v.One()Print v.Zero()Print v.Angle2()Print v.Angle2(10.0)Print v.Angle '<--- Error : Property 'Angle' cannot be accessed from an instance of a different classv.Angle = 2.0Print vPrint v.Len '<--- Error : Property 'Len' cannot be accessed from an instance of a different classv.Len = 30.0Print vEndNovember 19, 2016 at 7:37 pm #5251Doing some tests…
Monkey123456789101112131415161718192021222324252627282930#Import "<std>"Using std..Struct TestPrivateField value:FloatEndStruct Test Extension' Mutator methods are allowed.Method SetValue(v:Float)value = vEnd' Accessor methods are allowed.Method GetValue:Float()Return valueEnd' Properties are not allowed.'Property GetSomething:String()' Return "Hello"'EndEndFunction Main()Local a := New Testa.SetValue(100)Print(a.GetValue())EndNovember 19, 2016 at 11:20 pm #5262trouble using extern classes with any kind of extention
using either extends or extention I get Error : Super class ‘default.Rect’ has no default constructor
there is a slight complication in that the only a (float,float) constructor, but it will say the same if there is a () constructor… - 
		AuthorPosts
 
You must be logged in to reply to this topic.