GMWolf
aka fel666
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!
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.
The double duck operator ?? Would allow us to test and evaluate all in one swoop using ternary like syntax
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:
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.
Etc.
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?
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.
Code:
a?foo // evaluates to true if 'a' contains variable 'foo'
if ( a?foo ) {
var n = a.foo;
}
Code:
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
a??bar();
This will allow us to write some really neat duck typed code.
For instance, on collision with a trigger object:
Code:
other??onCollision();
Another example is to use the duck operator to find out the type of a lwo:
Code:
if (o?sprite && o?position)
{
//This is a thing we can render!
}
If it has a sprite and a position, we can draw it.
If it has health, we can damage it.
Etc.
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: