-
-
Notifications
You must be signed in to change notification settings - Fork 99
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
Add static type hints for array members #192
Comments
And for Dictionary members. |
This will require GDScript to support typed instructions first. Otherwise, the typing will just be used to improve autocompletion, type safety and documentation. |
I see. It would also still help for what you said at the end of your message which is a good advantage as well |
Other syntax ideas?
|
I've always been a fan of how Ruby YARD documentation handles Arrays and Dictionaries (called "Hashes" in Ruby, but I'll be calling them Dictionaries for simplicity). Perhaps we could use that syntax? For reference, Arrays are usually handled like As for Dictionaries, YARD documentation would usually look something like And the two can be put within each other: I don't know how hard it would be to implement this sort of syntax, but that's my suggestion for handling static typing for arrays and dictionaries. |
Once godotengine/godot/pull/43004 gets merged hopefully work on this can be started. |
I definitely want to make this for Godot 4.0. It still will take sometime since I have other tasks. |
I've been thinking about this just recently and then stumbled upon this issue. First I was thinking like C++ syntax for this, but I really like @willnationsdev 's idea, and I think that would be the best, and it still looks like GDScript. |
Go has |
@dgellow Yeah, I often like that syntax actually (C-like too). But how would you do Dictionaries then? |
For dictionaries, two options that build on my previous comment, and don't require additional characters (so shouldn't complicate the parsing rules too much, hopefully):
I find the second one a bit more clear, but I'm using Go quite a lot so that could be a bias. |
I don't want to open a pandoras box, but have you looked more into typescript? specially the way they handle:
let a: number[]; // [1, 2, 3]
let b: string[]; // ["a", "b", "c"]
let t: [string, number, boolean][]; // [["a", 1, true],["b", 2, false]]
let foo: number | string | null;
let bar: "up" | "left" | "right";
const enum Directions {
up = "KeyW",
left = "KeyA",
right = "KeyD"
}
let bar2: keyof Directions; // same as : "up" | "left" | "right";
let bar3: { [key: string]: number }; // { "x": 4 }
let foo = { bar: 42 }
let bar: typeof foo;
again - I know, this is already drifting far from array typing, and I am not saying Godot should take over all those syntax options - but I just wanted to list some of them, as I don't see them covered so far. |
I also find this very pleasing to the eye. Although, I'm likely biased due to PHP like you are of the latter version for Go. Either way, you think of the braces as wrapping the key ("what type is the key that goes in the square brackets?") or the value ("what type am I indexing to get?"), but both approaches work well imo. Between the two, I prefer the former because I like starting to type with actual type information rather than syntax characters. Makes it easier for me to just go with my train of thought. Need "X" which maps to "Y" vs. Need a "Map" of "X" to "Y". Slightly less cognitive load.
Supporting any of these would mean adding support for tuples and unions in the core Variant type which would likely be a lot more work. If you think adding these types provide a significant enough advantage to warrant integrating and maintaining them in the core, then you should open proposals for them. As it stands, I think the
Besides the "starting with syntax characters" thing I mentioned before, I especially don't like the idea of needing to use
Really not sure about this one. Sounds more GDScript-like to formally declare the existence of a type alias (if support for that were added) and then allowing you to use that in type hints. There's already I'm good with structs eventually being supported, cause tuples are convenient and useful, but as for everything else, I'd just keep to the |
To me, this is the only right way because you index dicts and arrays with |
I think Python's |
so wait you guys would perfer..
over?
how would something like constraints work if they were added? EDIT: TBH the first one looks better in python I think that we shouldn't do it like so.. I personally think something like this should be done...
|
Hi everyone.... I'm personally interested in this feature since I come from a Java background.
Since the current typing (3.1+) gives us this syntax...
...the syntax for the type hints following the same guidelines (IMO) should be like this...
And maybe, given the fact that dictionaries and arrays uses different symbols (
(Edit: Fixed a typo) |
Previously, code just assumed that the dictionary conformed to the structure outlined in state.gd. With the addition of the Player class, properties can be typed and well defined. Now, if only Godot had types for array members... godotengine/godot-proposals#192
Typed GDScript arrays were implemented by godotengine/godot#46830, closing. See #56 for dictionary type hints (which are not planned for 4.0, but may be added in a future release). |
Previously, code just assumed that the dictionary conformed to the structure outlined in state.gd. With the addition of the Player class, properties can be typed and well defined. Now, if only Godot had types for array members... godotengine/godot-proposals#192
Describe the project you are working on:
I am working on a Pokemon inspired RPG where the player would be a "manager" and would have battle moves and stats based on the objects they own
Describe the problem or limitation you are having in your project:
I guess it isn't a "problem" but I thought of something while implementing the party system.
While implementing the party system, I've made an array that would contain multiple instances of a specific class I made (BattleCharacter), however, while I always use static typing, it seems to be impossible to determine in advance the type all array members would have.
Which I am used to coming from a C# background, I also feel like it would allow a lot of optimizations in case of iterations through array members
Show a mock up screenshots/video or a flow diagram explaining how your proposal will work:
My proposal would be to allow something like
var array_name : type = []
or
var array_name = [] : type
it would then only allow to set values of the array to values of the specified type
Describe implementation detail for your proposal (in code), if possible:
As I only started using Godot recently, I unfortunately don't know enough the way the engine handles internally GDScript.
If this enhancement will not be used often, can it be worked around with a few lines of script?:
Well, yeah since it can be done without static typing, it's mostly a feature improving on the static typing added to 3.0
Is there a reason why this should be core and not an add-on in the asset library?:
It would allow big performance optimizations in games relying heavily on arrays and iterations.
The text was updated successfully, but these errors were encountered: