Discussion The duck operator

Discussion in 'GameMaker Studio 2 Community Tech Support' started by GMWolf, Aug 9, 2019.

  1. GMWolf

    GMWolf aka fel666

    Jun 21, 2016
    With a new version of GML around the corner with support of functions, methods, etc, I think it would be a great time to actually make use of GM's dynamic typing and turn it into a duck typing powerhouse!

    wlt8alg.png Duck typing suggestion

    The duck type operator would allow use to easily query if an object/lwo has a given variable.
    This would have the advantage of being easier to type, read, and offer early hashing performance improvements over current string based solutions.
    I suggest the use of '?' as an operator.
    a?foo // evaluates to true if 'a' contains variable 'foo'
    if ( a?foo ) {
        var n = a.foo;
    The double duck operator ?? Would allow us to test and evaluate all in one swoop using ternary like syntax
    a??foo : b; //evaluates to a.foo if a contains foo. B otherwise.
    var b = a??bar() : undefined; //evaluates a.bar if a has bar. Undefined otherwise.
    //As a shortcut you could leave out the ' : undefined' part when you don't care about the return result. Returns undefined by default

    This will allow us to write some really neat duck typed code.
    For instance, on collision with a trigger object:

    If the object we collided with has an onCollision method, we can run it. Otherwise it doesn't do anything.

    Another example is to use the duck operator to find out the type of a lwo:
    if (o?sprite && o?position)
      //This is a thing we can render!
    This way we don't need to rely on messy inheritance, but can directly test if an LWO can be used in some way simply by looking at which variables it has defined.
    If it has a sprite and a position, we can draw it.
    If it has health, we can damage it.

    The introduction of these new operators will not introduce any fundamentally new functionality we could not do without, but it would help shape the way we write GML.
    And I believe that healthy use of duck typing is the right way to go for GM.

    What are your thoughts? Have a missed something? Do you have anything you would like to add to this suggestion?
    Should YYG implement this for the next GML update?
    Last edited: Aug 9, 2019
  2. Tthecreator

    Tthecreator Your Creator!

    Jun 20, 2016
    This reminds me of Kotlin's safe call operator: https://kotlinlang.org/docs/reference/null-safety.html#safe-calls
    The operator is actually quite useful and widely used. I've worked with ti and I like it so I approve of this suggestion.
    The other !! (not null assertion) operator (which checks if a value is not null and throws an error if is), wouldn't be that much needed as the safe call operator since most gm functions already check this, and game maker throws errors itself. The upside to implementing a not null assertion operator is to be able to find false value higher up in the call stack, not until we get to a gm function. This makes debugging easier.
    GMWolf likes this.
  3. curato

    curato Member

    Jun 30, 2016
    I am not sure Yoyo is going to allow any kind of type checking that doesn't comply with community standards :p. All joking aside, I am all for stronger typing. Also, I would like to be able to strongly type all my variables instead of leaving them as variants.
    Cloaked Games likes this.
  4. YellowAfterlife

    YellowAfterlife ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ Forum Staff Moderator

    Apr 21, 2016
    I think ActionScript-style `<field name> in <object>` would be more appropriate for GML and slightly more flexible. I commonly implement a variation of it in scripting systems that I write for games
  5. GMWolf

    GMWolf aka fel666

    Jun 21, 2016
    Yeah that may be more appropriate as it does mean less chance of it being ambiguity with the ternary operator.

    Though what about the ?? Operator. I'm quite fond of reducing the number one line if statement bodies...
    Last edited: Aug 9, 2019

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice