One more Extension example

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.

Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #11471

    nerobot
    Participant

    We can often see such methods:

    We can hide null-checking into extension:

    And use it like this:

    Now we ask image to draw itself into canvas. 🙂

    Just a playing around.

    #11474

    Diffrenzy
    Keymaster

    Why the “If Self” ?

    #11486

    nerobot
    Participant

    Self can be null inside of extension.

    #11487

    Mark Sibly
    Keymaster

    Self 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.

    #11488

    nerobot
    Participant

    It’s a feature of extensions!

    We can write “safe” methods with that. Not only checking for nulls:

    Below is an example app to see Self is Null inside of extension:

    #11489

    nerobot
    Participant

    Since v1.1.08 we can get a similar result for some cases with ?.operator :

    _img?.Draw( … ) but it’s overhead in this case.

    #11495

    Diffrenzy
    Keymaster

    Ohh!, I see, but I’m not sure Monkey should behave like that.

    Self in a Method should always return an object IMO.

    #11497

    nerobot
    Participant

    Yes 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.

    #11501

    Mark Sibly
    Keymaster

    But 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…

    #11506

    abakobo
    Participant

    Yes, 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.

    #11507

    scurty
    Participant

    Oh the complications of scope. xD
    What about Self.Ptr? Dereference it to get the Object.

    #11514

    nerobot
    Participant

    But 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.

Viewing 12 posts - 1 through 12 (of 12 total)

You must be logged in to reply to this topic.