About Monkey 2 › Forums › Monkey 2 Programming Help › One more Extension example
This topic contains 11 replies, has 5 voices, and was last updated by
nerobot 1 year, 5 months ago.
-
AuthorPosts
-
November 4, 2017 at 12:43 pm #11471
We can often see such methods:
Monkey123Method Render( canvas:Canvas )if image canvas.DrawImage( image,x,y )EndWe can hide null-checking into extension:
Monkey12345678Class Image ExtensionMethod Draw( canvas:Canvas,x:Float,y:Float )if Self canvas.DrawImage( Self,x,y )EndEndAnd use it like this:
Monkey1image.Draw( canvas,0,0 )Now we ask image to draw itself into canvas.
Just a playing around.
November 4, 2017 at 7:01 pm #11474Why the “If Self” ?
November 5, 2017 at 4:10 am #11486Self can be null inside of extension.
November 5, 2017 at 6:00 am #11487Self can be null inside of extension.
I never really considered this, not sure if I like it or not!
[edit] Don’t think I do like it, esp now there’s ‘?.’ – please don’t depend on it in future.
November 5, 2017 at 6:10 am #11488It’s a feature of extensions!
We can write “safe” methods with that. Not only checking for nulls:
Monkey1234567' extension for some our class, likeMethod GetSafelySessionId()If Not Self or session.disposed Return -1Return session.idEndBelow is an example app to see Self is Null inside of extension:
Monkey12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849Namespace myapp#Import "<std>"#Import "<mojo>"Using std..Using mojo..Class MyWindow Extends WindowMethod New( title:String="Simple mojo app",width:Int=640,height:Int=480,flags:WindowFlags=Null )Super.New( title,width,height,flags )EndMethod OnRender( canvas:Canvas ) OverrideApp.RequestRender()canvas.DrawText( "Hello World!",Width/2,Height/2,.5,.5 )_img.Draw( canvas,50,50 )EndField _img:Image ' don't assign to test extensionEndFunction Main()New AppInstanceNew MyWindowApp.Run()EndClass Image ExtensionMethod Draw( canvas:Canvas,x:Float,y:Float )If Selfcanvas.DrawImage( Self,x,y )ElsePrint "image is null"EndifEndEndNovember 5, 2017 at 6:12 am #11489Since v1.1.08 we can get a similar result for some cases with ?.operator :
_img?.Draw( … ) but it’s overhead in this case.
November 5, 2017 at 10:12 am #11495Ohh!, I see, but I’m not sure Monkey should behave like that.
Self in a Method should always return an object IMO.
November 5, 2017 at 1:18 pm #11497Yes it should. Self in extension not equal to self in usual class.
Extension methods converted into plain functions by mx2cc. I think we need to have explanation of it in docs if have no yet.
November 5, 2017 at 6:48 pm #11501But how does user ‘know’ they are using an extension and it’s safe to skip ‘?.’?
If there’s a possiblility an object could be Null, coder should always use ?. or check for null IMO, regardless of whether member is an extension or not. Extensions should behave as much like ‘normal’ methods as possible.
Making a special case for extensions just complicates the language more. My current thinking anyway…
November 5, 2017 at 7:57 pm #11506Yes, extension should behave just the same as classes/struct and a framework end-user shouldn’t need to pay attention to that kind of things IMHO.
November 5, 2017 at 8:33 pm #11507Oh the complications of scope. xD
What about Self.Ptr? Dereference it to get the Object.November 6, 2017 at 4:43 am #11514But how does user ‘know’ they are using an extension and it’s safe to skip ‘?.’?
If there’s a possiblility an object could be Null, coder should always use ?. or check for null IMO, regardless of whether member is an extension or not. Extensions should, IMO , behave as much like ‘normal’ methods as possible.
I agree.
Main rule here: all code – including extensions – should be fully documented!
I don’t invite you to write all null-checks inside of extensions, I just shown the way we *could* to use it.
There is a String.IsNullOrEmpty() extension method in c# to eliminate null checking until it has ?. operator that comes with c# v6.0.
-
AuthorPosts
You must be logged in to reply to this topic.