Function pointers should auto-cast to Bool

About Monkey 2 Forums Monkey 2 Development Function pointers should auto-cast to Bool

This topic contains 3 replies, has 3 voices, and was last updated by  sicilica 2 years, 9 months ago.

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • #1850

    sicilica
    Participant

    This is a pretty simple thing, and I’m sure someone will disagree with me, but it would be a really nice quality of life improvement if Function types implicitly cast to Bools.

    This works with objects, of course. Compare this:
    <pre class=”lang:monkey decode:true “>Class FooBar

    Field entity:Entity

    Method Update()
    If entity
    entity.Update()
    Endif
    End

    End

    And this:
    <pre class=”lang:monkey decode:true”>Class FooBar

    Field updateCallback:Void()

    Method Update()
    If updateCallback <> Null ‘ doesn’t work without “<> Null”
    updateCallback()
    Endif
    End

    End

    It seems silly that I have to explicitly tell the compiler what I mean, since what it means for a function pointer to be “truthy” is pretty well understood.

    #1860

    Matthew Smith
    Participant

    I believe the ? operator will be added soon which will verify if an object is null before using it:
    http://monkey2.monkey-x.com/monkey2-roadmap/

    so you could do entity?.Update() in you example.

    #1861

    Mark Sibly
    Keymaster

    He’s talking about something different here, ie: whether a function pointer is null or not, not an object.

    I have no general problem with this change – I think it was disabled because it confused the c++ compiler but it should be easy enough to fix.

     

    Another thing to note is that you can actually call a ‘null’ function pointer – it will perform a ‘NOP’ and return a Null value of the return type.

    #1867

    sicilica
    Participant

    You can call null functions? That’s – interesting. I can’t think of a lot of reasons that’d be useful, but it’s good to know.

    The object ?. operator would be a lot like Option objects in languages like Scala, it sounds like. I guess having both that and a default behavior for null function calls could clean up a lot of boilerplate safety checks. Sweet.

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

You must be logged in to reply to this topic.