-
-
Notifications
You must be signed in to change notification settings - Fork 21.9k
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
Unify typing of variables, constants and parameters in GDScript #70464
Conversation
921cbf0
to
d7324a6
Compare
a7b04a1
to
c461c84
Compare
Added simple test to show that implicit conversion now works for arguments: func check(arg: float = 3): # would have failed before
return typeof(arg) == typeof(3.0) Updated to include fix for null as default type of arguments/variables with corresponding test: func check(input: int) -> bool:
return input == 1
var recur = null
var prop = null
func check_arg(arg = null) -> void:
if arg != null:
print(check(arg)) # would have failed before
func check_recur() -> void:
if recur != null:
print(check(recur)) # would have failed before
else:
recur = 1
check_recur()
func test() -> void:
check_arg(1)
check_recur()
if prop == null:
set('prop', 1)
print(check(prop)) # would have failed before
set('prop', null)
var loop = null
while loop != 2:
if loop != null:
print(check(loop)) # would have failed before
loop = 1 if loop == null else 2 I know from this comment that there is a discussion to stop inferring type of arguments from default values. I see two ways:
Can remove that change from PR. It is a one-line difference. |
c461c84
to
e74b0d0
Compare
e74b0d0
to
6aaa69b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems a good approach to me.
6aaa69b
to
a1d0674
Compare
Thanks! This is a huge amount of bugs fixed in one fell swoop :) |
Whups - just found this PR after filing the bug - #70978. The following code now crashes: func _ready():
# This works
test([1, 1, 1])
# This crashes
test()
func test(array := [1, 2, 3]):
print("array is ", array) |
Member variables, local variables, member constants, local constants and parameters - all of those have almost the same code for determining a type. This PR tries to unify that logic under one function, fix inconsistencies and to show explicitly what are the differences between those types.
Introduced
GDScriptParser::AssignableNode
as a parent forVariableNode
,ConstantNode
andParameterNode
. They all have the same properties that used by the resolving logic:The only difference is that in
ParameterNode
initializer
was calleddefault_value
. If this rename is a deal breaker - can removeAssignableNode
and pass those properties one by one instead.Otherwise pretty-straightforward.
Notes:
A variable has
use_conversion_assign
, strange that same functionality is not exposed to a parameter.I used construction like
static constexpr const char* kind = "variable";
inside a function to pass a string used in errors. Did so to avoid allocations/conversions. If this does not matter and I should useString
instead - just let me know.Next issue(s) about typing of null as default value is fixed (example in next comment):
Fixes #53115.
Fixes #56217.
Fixes #67048.
Fixes #70523.
Next issue(s) connected to assignment of weak default value to typed variable is fixed:
Fixes #66141.
Fixes #70912.