-
-
Notifications
You must be signed in to change notification settings - Fork 21.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Optional typing in GDScript #10630
Comments
It looks like you can do func myfunction(something = InputEvent): and that will give you an empty input event if they don't pass anything, but doesn't enforce the variable to be that type myfunction('string') would work fine. |
It's on the roadmap :) |
@Hinsbart |
Unfortunately no, there aren't any references for it yet, besides some irc discussions. CC @reduz, to give some hints of what he has in mind for this. |
@LeonardMeagher2 should look like what you wrote |
What about: func myfunction(something is InputEvent, something_else is String): |
That's pretty good to me, makes the is keyword more useful and the code easier to understand |
I disagree. I would be fine with either |
If |
@neikeq Otherwise I think it would be more confusing to use the semicolon in just function definitions. "Is" is used to ask about inheritance in gdscript now, so why couldn't it require it? |
C like type prefix would be better IMO. It is the shorter, faster to type and can replace var or func when using types. Example:
My vote on that. |
@juan-garcia-m Obviously the language can't be made for me, but I'm sure I'm not the only one that feels this way. |
One problem with "is", it's currently used in 3.0 for what used to be "extends", i.e. checking if a node belongs to a class. |
@Zireael07 |
C style prefix is more complicated in general, and for these types of languages the standard seems to be the variable : type convention, so that will be used |
Why was this closed? It is still not implemented... |
I closed it because it was just a discussion, but I'll reopen it to track the progress of the desired feature |
I really do hope that the C style prefix gets reconsidered. From my experience with Typescript, I feel less productive when initialising variables and I personally feel like it results in less readable code. Having to write this...
...instead of just replacing the
...drives me crazy. I'm aware that this sounds very small and nit-picky, but over the long term of working on a game this just results in way more keystrokes than what's necessary. |
Following the Python syntax (also similar to TypeScript and Haxe). This is quite opinionated. For instance, I wrote a lot of TypeScript code and really like the syntax. Like tabs vs. spaces, everyone will have their own opinion of what's is best, usually related to their background. I find it easier to see Another point pro the Python way: it's much easier to parse. The current parser won't have to change much to accommodate this. For the prefixed types, it'll have to consider more stuff about the context to decide what to do. |
Yeah code readability is very subjective. I rewrote your example in how it would look like with the C style syntax. I find the code below more readable and I have no trouble differentiating between variables and functions. extends Node
const int MY_CONSTANT = 0
String my_member_variable = "Value"
void my_empty_func():
pass
float my_other_func(bool arg1, Node2D arg2, bool default = false):
Vector2 local_variable
int other_variable = 0
return float(other_variable) I have to disagree with "faster to type" not being a good argument as it directly affects a developers productivity. The Python style is slightly more cumbersome to read and write than C's is, even more so now seeing we will have to type Anyway, that's just my two pennies' worth 😄. I'm enjoying developing in GDScript so far and I'm looking forward to it having a sound type system. |
Python already supports this, so I think the best we can do is copy from there |
For people who hate the var keyword it could be possible to add the Nim like syntax who allow something like this.
|
I wonder why return types are needed, if you are returning and that output is going to a variable of a specific type and they don't match, it'd throw then. The return type Finally the Did not mean to close the issue |
I can see return types being known to be important in some cases since it will now be possible to read every function for a Node, its variables, and its return type. It may not make a lot of sense now, but people will be able to write plugins and even game code that can be extremely dynamic in how it works. IE, a plugin could have this warning message: "This Node requires a function with x return type, but the Node you're trying to reference has no method that has the required method signature." I wrote a plugin for Unity called AI Behavior that uses quite a bit of this type of stuff (via System.Reflection) for user friendliness. I literally have a drop down menu I populate with available method names and let them select which method they want to use as a callback based on all the methods that have the correct signature, then when the game runs, that's the method that gets called under the circumstance it's made for. If no method exists on the object/script they're trying to access it on then it will show sample code in the inspector so they can just make a script via copy/pasting the code and renaming the class. :) So, my thought is that being able to have as much script info as possible will make Godot that much more extensible for plugin writers. |
I also think TypeScript style syntax (using Also, following python syntax is a good idea: This will help people coming to GDScript from similar dynamic languages with optional typing. BTW, when / in what release can we expect this to be implemented? I've seen 3.1 mentioned? |
This should be available in Godot 3.1. |
About the arguments against C style prefix being more complicated or less understandable/readable and it being more welcoming to go with python syntax or similar—aren't the docs all using that C style sort of prefix with all methods and such? Like, just to pull one off the Camera page really quick |
Hi, proposal here. Rationale: https://crystal-lang.org/docs/ to keep backward compatibility some new keyword will be needed for defining functions and variables. Or maybe we could have some magic comment to enable/disable static type checking. Here is an example of what this kind of type system can do (for the ones who know a bit of ruby)
output:
Thoughts? |
I do want to improve the type inference, especially for the code completion but also for checking non-existing properties/methods and invalid argument count at editing time. I think this part of a second step after optional typing is done, so I'm not fretting myself over it for now. The example from @m4nu3lf goes quite far, it's actually inferring the resulting type based on the branching. I can't see how that's done without simulating the execution of the code, where it can get quite complex. I'm not sure how far @reduz wants to go with GDScript. Consider that Godot has a quite strict type system, but all variables are Variant (i.e. they can store any type). Since this is not meant to break compatibility, they have to remain like that. So doing One possibility, as mentioned, is to make this optional. Maybe a project setting, since I don't see much value of this being enabled/disabled per-script. Still, that's a separate proposal to be thought in a next step. |
@vnen In theory by using some new keyword for parameters/return types/variables like 'auto' or 'let' and using the same algorithm of crystal-lang you could be able to infer the type at compile time. However that language goes beyond simple variables. It can restrict the type to a subset of types depending on the branches.
Furthermore you must check for "instanceof" conditions and like in
then you know that in the body of the if condition my_var can only be 'MyClass' so the type is restricted even more.
Then after this x is either a MyClass or a String. you will only be able to call methods that are common to both to avoid runtime errors. I give that this is a lot more work of what I originally thought. |
@m4nu3lf I understand what your request is, TypeScript is very similar (which is a language I'm more familiar with). However, there's a few problems to consider for GDScript:
This is already inferred for code completion. Could be reused for actual type-checking. This case in particular is trivial, but it can get quite involved. For instance, if you add a second condition: I also want to hear feedback from users, so have something available soon is more important (to me) than have everything perfect after a long wait time. Summing up: I do want to improve type inference, but it has to be made step by step, with small improvements. Once optional typing is available, the rest is uphill. |
Why is this closed? |
Misread a bit, my bad. |
Back to prefix and postfix: UML and Kotlin both use ": type". |
@programaths It has been a long time since, and it has been discussed over and over. What's more, there is a PR which implements postfix, so it is a bit out of the question now 😃 |
Should I close this? |
@LeonardMeagher2 it'll be closed automatically when my PR is merged. |
@bojidar-bg I just wanted to add few places where postfix are used because it's interesting by itself. UML is meant for analysis and non-technical people too. Kotlin is a language which was developped to be more comfortable and more modern than Java while implementing "effective java". So, if you need to write down why postfix has been chosen, these can be included too. Sometimes, people want to have reasons and easily dismiss technicals ones unless you relate them to the user. Always good to have material, isn't it ? ;-) |
I don't know if this would be needed or interesting to any developer, but I think it might be useful to have optional typing in GDScript and was hoping to have a discussion about it here.
I've been learning the programming language called "Nim" because it has a pretty friendly module for GDNative and I'm interesting in learning to contribute there (since C++ is difficult for me visually BUT unrelated to this, just some background). Nim is statically typed which I know GDSCript is dynamic but I came across a situation in which I might want to specify type to make things easier.
Mainly when writing a function, it'd be nice if I could specify the type of a variable that is allowed to be passed in so the code completion could know what I'm talking about, but I'm sure there are other advantages to this.
Something like
Where a is dynamic, b-d are static but show what I think the syntax could look like.
I know you can specify type hint for the editor when using the export keyword, but you don't have to keep the variable at that type once the script executes. This idea could be against what GDScript is meant to be, or too much work for very little gain, but lets talk.
The text was updated successfully, but these errors were encountered: