Is specifying type possible in GDScript?

snwflksnwflk Posts: 25Member

I'm learning GDScript and I find it very friendly and straightforward. It has one big drawback that cancels out those benefits: dynamic typing. it simplifies things in the same manner as having float for any numeric type you'd need. No worries, no headaches, right?
Well, it may make things easier with declaring and assigning, for the one that writes the code but it creates guaranteed confusion for the readers.
Specifying the type for a variable says a lot about its purpose, complementing the name.
Not specifying the declaring type is like reading in a monochrome code editor where you need experience with the syntax and API. Additionally you'd have to remember what type each of the variables you're working with, is. You still have to use them properly, it's not like you could mix them without discernment.

Consider this snippet from the very official documentation:

 1. var collision = move_and_collide(velocity * delta)
 2. if collision:
 3.     var reflect = collision.remainder.bounce(collision.normal)
 4.     velocity = velocity.bounce(collision.normal)
 5.     move_and_collide(reflect)

You can't cut your way through the code in a glance. Here's what I'm experiencing line by line:

1) Not interested in velocity but what is collision? I don't know so I'll search the doc for the return value of move_and_collide(). Or I'll just wait and see later from the context.

2) Oh, it could be a class, but from the context maybe it's a bool. So probably move_and_collide() returns a bool for colliding or not.

3) What's reflect? An amount as float? A vector as direction? I'll see later.

4) What's velocity? It appears to be the same as whatever reflect is, due to being passed into the same function, bounce().
I know what bounce() generally does but I can't place it in this code. Maybe it's time to go to the manual to see what type bounce() takes. And what type it returns.

5) OK, so the kinematic 2D node is moving with collision check, again.

Now, if you argue you have to know the API very well, I argue that it's even more common sense that you have to know the types you're using.
And you might expect from a game to contain, besides API functions, user written functions, right? Now the code gets obstructively unbrowsable.

So, is there a way to specify the type? At least as a future addition to GDScript. I don't mean strong typing as in enforcing the type but merely as a visual cue.

Tags :


Sign In or Register to comment.