Skip to content
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 an optional warning to type inferred variables and parameters #59428

Closed
wants to merge 1 commit into from

Conversation

jordigcs
Copy link
Contributor

@jordigcs jordigcs commented Mar 23, 2022

Relies on #59943

Closes godotengine/godot-proposals#173 and godotengine/godot-proposals#3531

Warnings are now enum values (Ignore, Warn, Error) to allow setting whether you want a warning to be treated as either an error, a warning, or ignored.

The way warnings are initialized in gdscript.cpp has been improved to offer more flexibility. (Support for property info per warning and different default values.)

Type Inferencing

Added new warnings that will let the user know if a variable or parameter they defined is not statically typed (Set to 'Ignore' by default)

Variables that are assigned to null when declared are ignored so that if you absolutely need to, you can still have a dynamically typed variable.

@jordigcs jordigcs requested review from a team as code owners March 23, 2022 05:11
@jordigcs
Copy link
Contributor Author

The checks are failing because the documentation hasn't been updated yet, I'll get them up to date tomorrow.

@jordigcs jordigcs force-pushed the inferred_type_warning branch 2 times, most recently from 0620c44 to ded90b8 Compare March 23, 2022 13:57
@jordigcs
Copy link
Contributor Author

The checks are failing because the documentation hasn't been updated yet, I'll get them up to date tomorrow.

Fixed 👍

@jordigcs
Copy link
Contributor Author

Changed the property info for 'Enforce Static Types' warnings to use 'Disabled' instead of 'Ignore' for the enum strings.

@jordigcs
Copy link
Contributor Author

Docs need to be updated, I'll get to it soon

@jordigcs jordigcs force-pushed the inferred_type_warning branch 4 times, most recently from bb674d0 to 79ca41c Compare March 29, 2022 16:50
@Calinou
Copy link
Member

Calinou commented Mar 29, 2022

Looks good!

In addition to the review comment above, I suggest also renaming "Enforce Static Parameter Types" to "Enforce Static Method Parameter Types" to be more explicit.

PS: It seems this pull request actually closes godotengine/godot-proposals#173, not godotengine/godot-proposals#3284.
godotengine/godot-proposals#3284 should be addressed with a separate pull request as it's less important to have, and might be a bit more involved to do. Either way, this is very welcome 🙂

@jordigcs
Copy link
Contributor Author

Looks good!

In addition to the review comment above, I suggest also renaming "Enforce Static Parameter Types" to "Enforce Static Method Parameter Types" to be more explicit.

PS: It seems this pull request actually closes godotengine/godot-proposals#173, not godotengine/godot-proposals#3284. godotengine/godot-proposals#3284 should be addressed with a separate pull request as it's less important to have, and might be a bit more involved to do. Either way, this is very welcome 🙂

Gotcha, I've renamed it and edited the referenced proposal. The docs should be correct now too.

@jordigcs jordigcs force-pushed the inferred_type_warning branch 2 times, most recently from 1a2644c to a8d3fda Compare March 31, 2022 19:46
@jordigcs jordigcs force-pushed the inferred_type_warning branch 2 times, most recently from ddb3051 to bbd4fa1 Compare April 6, 2022 15:51
@AaronRecord
Copy link
Contributor

Variables that are assigned to null when declared are ignored so that if you absolutely need to, you can still have a dynamically typed variable.

Correct me if I'm wrong, but I think you could also specify the type as Variant

@AaronRecord

This comment was marked as off-topic.

@jordigcs
Copy link
Contributor Author

We discussed this in a GDScript meeting with @vnen and @Calinou, here are some suggestions:

  • The first commit implementing enum values for warnings seems fine, and would be best moved to its own dedicated PR so that it can be merged independently of the static typing option.

  • For the static typing options, some more discussion might be warranted regarding:

    • Name of the category (it's reusing the warning system but special cased as a different category, that's a bit weird)
    • Whether two options should be added or just one for all cases

I separated the enum warnings commit to #59943.

@jordigcs jordigcs force-pushed the inferred_type_warning branch 2 times, most recently from d46e269 to c38cc24 Compare May 25, 2022 18:28
@jordigcs
Copy link
Contributor Author

I also see that this is doing a bit different from the proposal, as it's forbidding type inference when it should be forbidding non-static types (unless I misread the code). Those are different things. If you use var x := 0 you are using type inference and static typing, it's important that this case is allowed when the user wants to enforce static types.

In fact, I don't think this needs mention type inference inference at all (unless it's to clarify that it is allowed).

Gotcha, annotated type inferencing is allowed now.
image

@jordigcs jordigcs force-pushed the inferred_type_warning branch 2 times, most recently from 1a008a9 to 3b1fb0e Compare May 25, 2022 18:43
@jordigcs
Copy link
Contributor Author

The checks are failing because the documentation uses int instead of bool. The int comes from #59943

@jordigcs
Copy link
Contributor Author

Rebased to be up to date with master.

@jtnicholl
Copy link
Contributor

jtnicholl commented Sep 11, 2022

The existing warnings are named after what triggers them, for example "shadowed member variable" or "int assigned to enum".
For these to be consistent they should be called "dynamically typed member variable" and "dynamically typed parameter", or something similar.

@jordigcs
Copy link
Contributor Author

Sounds like a good idea. I'll change it along with any other needed changes.

@DasGandlaf
Copy link

Waiting for this to get merged 👀

@jordigcs
Copy link
Contributor Author

I'm not sure if this would be merged right now because of the feature freeze. I believe the changes from the PR review have been implemented, so I think this is just waiting to be reviewed again.

@Calinou Calinou modified the milestones: 4.0, 4.x Dec 17, 2022
@hilfazer
Copy link
Contributor

hilfazer commented Aug 3, 2023

What about polymorphic types? Will this PR produce a warning in the last line of the following code:

extends Node


func _ready():
	var _q :Node2D = $'.'	# runtime error if the value doesn't inherit Node2D

Static typing would result in compile time error here but what GDScript does is a runtime type check (which may or may not result in an error).

I'm asking because this PR is allegedly adressing godotengine/godot-proposals#173.

@GiverofMemory
Copy link

This is probably obvious but does typing GDScript statically improve performance? Is that the main reason for this? If so, I think this would be an excellent feature as people can get the GDScript integration combined with the speed of a statically typed language like C#.

More discussion: https://www.reddit.com/r/godot/comments/vj4cs8/is_there_a_way_to_force_static_typing_in_godot_40/

@Calinou
Copy link
Member

Calinou commented Sep 18, 2023

This is probably obvious but does typing GDScript statically improve performance?

Since 4.0, it does thanks to typed instructions. Prior to 4.0, there was no performance impact whatsoever.

Is that the main reason for this?

The main reason is easier code maintenance and better autocompletion, rather than performance. People like TypeScript a lot, yet it doesn't make your code any faster than JavaScript 🙂

@dalexeev
Copy link
Member

Superseded by:

Thanks for the contribution nonetheless!

@dalexeev dalexeev closed this Sep 22, 2023
@dalexeev dalexeev removed this from the 4.x milestone Sep 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow users to choose if scripts should be forced to be statically typed (or not)
10 participants