You've already forked godot
mirror of
https://github.com/godotengine/godot.git
synced 2025-11-06 12:20:30 +00:00
Merge pull request #82808 from dalexeev/gds-vararg
GDScript: Add support for variadic functions
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
class A:
|
||||
func f1(x: int, ...args: Array) -> void:
|
||||
prints(x, args)
|
||||
|
||||
func f2(x: int, ...args: Array) -> void:
|
||||
prints(x, args)
|
||||
|
||||
class B extends A:
|
||||
func f1(x: int, y: int, ...args: Array) -> void:
|
||||
prints(x, y, args)
|
||||
|
||||
func f2(x: int) -> void:
|
||||
print(x)
|
||||
|
||||
func g(...args: int):
|
||||
pass
|
||||
|
||||
func h(...args: Array[int]):
|
||||
pass
|
||||
|
||||
func test():
|
||||
pass
|
||||
@@ -0,0 +1,5 @@
|
||||
GDTEST_ANALYZER_ERROR
|
||||
>> ERROR at line 15: The rest parameter type must be "Array", but "int" is specified.
|
||||
>> ERROR at line 18: Typed arrays are currently not supported for the rest parameter.
|
||||
>> ERROR at line 9: The function signature doesn't match the parent. Parent signature is "f1(int, ...) -> void".
|
||||
>> ERROR at line 12: The function signature doesn't match the parent. Parent signature is "f2(int, ...) -> void".
|
||||
@@ -0,0 +1,5 @@
|
||||
func f(...args, extra_arg):
|
||||
pass
|
||||
|
||||
func test():
|
||||
pass
|
||||
@@ -0,0 +1,2 @@
|
||||
GDTEST_PARSER_ERROR
|
||||
Cannot have parameters after the rest parameter.
|
||||
@@ -0,0 +1,5 @@
|
||||
func f(...args, ...more_args):
|
||||
pass
|
||||
|
||||
func test():
|
||||
pass
|
||||
@@ -0,0 +1,2 @@
|
||||
GDTEST_PARSER_ERROR
|
||||
Cannot have parameters after the rest parameter.
|
||||
@@ -0,0 +1,5 @@
|
||||
func f(...args = []):
|
||||
pass
|
||||
|
||||
func test():
|
||||
pass
|
||||
@@ -0,0 +1,2 @@
|
||||
GDTEST_PARSER_ERROR
|
||||
The rest parameter cannot have a default value.
|
||||
@@ -0,0 +1,5 @@
|
||||
static func _static_init(...args) -> void:
|
||||
pass
|
||||
|
||||
func test():
|
||||
pass
|
||||
@@ -0,0 +1,2 @@
|
||||
GDTEST_PARSER_ERROR
|
||||
Static constructor cannot have parameters.
|
||||
@@ -4,6 +4,7 @@ abstract class A:
|
||||
|
||||
# No `UNUSED_PARAMETER` warning.
|
||||
abstract func func_with_param(param: int) -> int
|
||||
abstract func func_with_rest_param(...args: Array) -> int
|
||||
abstract func func_with_semicolon() -> int;
|
||||
abstract func func_1() -> int; abstract func func_2() -> int
|
||||
abstract func func_without_return_type()
|
||||
@@ -23,6 +24,7 @@ class C extends B:
|
||||
return "text_2c"
|
||||
|
||||
func func_with_param(param: int) -> int: return param
|
||||
func func_with_rest_param(...args: Array) -> int: return args.size()
|
||||
func func_with_semicolon() -> int: return 0
|
||||
func func_1() -> int: return 0
|
||||
func func_2() -> int: return 0
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
class A:
|
||||
func f(x: int) -> void:
|
||||
print(x)
|
||||
|
||||
class B extends A:
|
||||
func f(x: int, ...args: Array) -> void:
|
||||
prints(x, args)
|
||||
|
||||
class C extends B:
|
||||
func f(x: int, y: int = 0, ...args: Array) -> void:
|
||||
prints(x, y, args)
|
||||
|
||||
class D extends C:
|
||||
func f(...args: Array) -> void:
|
||||
print(args)
|
||||
|
||||
func test_func(x: int, y: int = 0, ...args: Array) -> void:
|
||||
prints(x, y, args)
|
||||
|
||||
var test_lambda := func (x: int, y: int = 0, ...args: Array) -> void:
|
||||
prints(x, y, args)
|
||||
|
||||
func test():
|
||||
for method in get_method_list():
|
||||
if str(method.name).begins_with("test_"):
|
||||
print(Utils.get_method_signature(method))
|
||||
|
||||
test_func(1)
|
||||
test_func(1, 2)
|
||||
test_func(1, 2, 3)
|
||||
test_func(1, 2, 3, 4)
|
||||
test_func(1, 2, 3, 4, 5)
|
||||
|
||||
test_lambda.call(1)
|
||||
test_lambda.call(1, 2)
|
||||
test_lambda.call(1, 2, 3)
|
||||
test_lambda.call(1, 2, 3, 4)
|
||||
test_lambda.call(1, 2, 3, 4, 5)
|
||||
@@ -0,0 +1,12 @@
|
||||
GDTEST_OK
|
||||
func test_func(x: int, y: int = 0, ...args) -> void
|
||||
1 0 []
|
||||
1 2 []
|
||||
1 2 [3]
|
||||
1 2 [3, 4]
|
||||
1 2 [3, 4, 5]
|
||||
1 0 []
|
||||
1 2 []
|
||||
1 2 [3]
|
||||
1 2 [3, 4]
|
||||
1 2 [3, 4, 5]
|
||||
@@ -117,6 +117,10 @@ static func get_method_signature(method: Dictionary, is_signal: bool = false) ->
|
||||
if i >= mandatory_argc:
|
||||
result += " = " + var_to_str(default_args[i - mandatory_argc])
|
||||
|
||||
if method.flags & METHOD_FLAG_VARARG:
|
||||
# `MethodInfo` does not support the rest parameter name.
|
||||
result += "...args" if args.is_empty() else ", ...args"
|
||||
|
||||
result += ")"
|
||||
if is_signal:
|
||||
if get_type(method.return, true) != "void":
|
||||
|
||||
Reference in New Issue
Block a user