You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Describe the problem or limitation you are having in your project
I want to have the full project statically typed for safety & performance.
var test :Array[int] = [1,2,10,4,5]
var max : int = test.max()
The second line will be gray and not by type-safe. It's not properly static typed because Array.max() returns a Variant.
This also isn't type-safe: var max : int = int(test.max())
Or this: var max : int = test.max() as int
Or this: var max : int = test.max() if test.max() else 0
Or this: reduce(func(max: int, i: int) -> int: return i if i > max else max, 0)
As you can see, all these "workarounds" still fail. By failing here I mean that they are not considered to be type-safe.
All the above also goes for min() (and probably for all of the built-in Array methods that return Variant).
Describe the feature / enhancement and how it helps to overcome the problem or limitation
Easier to get static typing everywhere, which is safer + better performance.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
In a perfect world, everything would be handled under the hood, so this would Just WorkTM:
var test :Array[int] = [1,2,10,4,5]
var max : int = test.max()
And both lines would be considered as type-safe.
Alternatives:
Minimal effort option: var max : int = test.max() as int
Since the underlying problem is that max() could return null when the Array is empty, we could address that issue directly by add a default value parameter to max(): var max : int = test.max(default=1000)
This way null could never happen and should be type-safe.
If this enhancement will not be used often, can it be worked around with a few lines of script?
This is "correct" (as in, type-safe with a green line number): var max : int = test[test.find(test.max())]
But I hope we can all agree this is a bit of a ridiculous/over-engineered solution for something that should be simple.
A very simple benchmark points to about 25~50% speed gain with only using max() compared to the above "solution":
var test :Array[int] = [1,2,3,10,9]
var loop := 1000000
var start := Time.get_ticks_usec()
for i in loop:
var max : int = test.max()
var end := Time.get_ticks_usec()
var start2 := Time.get_ticks_usec()
for i in loop:
var max2 : int = test[test.find(test.max())]
var end2 := Time.get_ticks_usec()
printt("Only max: ", end-start)
printt("Correct: ", end2-start2)
To put it simply, the core currently does not provide information about which parameter and return types are generic and which are not (we cannot assume that Variant is always a generic placeholder). To look at it in more detail, Godot does not have a unified type system and the ability to represent a type as a value.
Describe the project you are working on
Any project using Array + built-in methods.
Describe the problem or limitation you are having in your project
I want to have the full project statically typed for safety & performance.
The second line will be gray and not by type-safe. It's not properly static typed because Array.max() returns a
Variant
.This also isn't type-safe:
var max : int = int(test.max())
Or this:
var max : int = test.max() as int
Or this:
var max : int = test.max() if test.max() else 0
Or this:
reduce(func(max: int, i: int) -> int: return i if i > max else max, 0)
As you can see, all these "workarounds" still fail. By failing here I mean that they are not considered to be type-safe.
All the above also goes for
min()
(and probably for all of the built-in Array methods that returnVariant
).Describe the feature / enhancement and how it helps to overcome the problem or limitation
Easier to get static typing everywhere, which is safer + better performance.
Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
In a perfect world, everything would be handled under the hood, so this would Just WorkTM:
And both lines would be considered as type-safe.
Alternatives:
Minimal effort option:
var max : int = test.max() as int
Since the underlying problem is that
max()
could returnnull
when the Array is empty, we could address that issue directly by add a default value parameter to max():var max : int = test.max(default=1000)
This way
null
could never happen and should be type-safe.If this enhancement will not be used often, can it be worked around with a few lines of script?
This is "correct" (as in, type-safe with a green line number):
var max : int = test[test.find(test.max())]
But I hope we can all agree this is a bit of a ridiculous/over-engineered solution for something that should be simple.
A very simple benchmark points to about 25~50% speed gain with only using
max()
compared to the above "solution":Output:
A potential alternative solution could be #162.
Is there a reason why this should be core and not an add-on in the asset library?
The required work would probably change internal types and methods.
The text was updated successfully, but these errors were encountered: