GDScript: Check if method signature matches the parent
To guarantee polymorphism, a method signature must be compatible with the parent. This checks if: 1. Return type is the same. 2. The subclass method takes at least the same amount of parameters. 3. The matching parameters have the same type. 4. If the subclass takes more parameters, all of the extra ones have a default value. 5. If the superclass has default values, so must have the subclass. There's a few test cases to ensure this holds up.
This commit is contained in:
+10
@@ -0,0 +1,10 @@
|
||||
func test():
|
||||
print("Shouldn't reach this")
|
||||
|
||||
class Parent:
|
||||
func my_function(_par1: int) -> int:
|
||||
return 0
|
||||
|
||||
class Child extends Parent:
|
||||
func my_function() -> int:
|
||||
return 0
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
GDTEST_ANALYZER_ERROR
|
||||
The function signature doesn't match the parent. Parent signature is "int my_function(int)".
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
func test():
|
||||
print("Shouldn't reach this")
|
||||
|
||||
class Parent:
|
||||
func my_function(_par1: int) -> int:
|
||||
return 0
|
||||
|
||||
class Child extends Parent:
|
||||
func my_function(_pary1: int, _par2: int) -> int:
|
||||
return 0
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
GDTEST_ANALYZER_ERROR
|
||||
The function signature doesn't match the parent. Parent signature is "int my_function(int)".
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
func test():
|
||||
print("Shouldn't reach this")
|
||||
|
||||
class Parent:
|
||||
func my_function(_par1: int = 0) -> int:
|
||||
return 0
|
||||
|
||||
class Child extends Parent:
|
||||
func my_function(_par1: int) -> int:
|
||||
return 0
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
GDTEST_ANALYZER_ERROR
|
||||
The function signature doesn't match the parent. Parent signature is "int my_function(int = default)".
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
func test():
|
||||
print("Shouldn't reach this")
|
||||
|
||||
class Parent:
|
||||
func my_function(_par1: int) -> int:
|
||||
return 0
|
||||
|
||||
class Child extends Parent:
|
||||
func my_function(_par1: Vector2) -> int:
|
||||
return 0
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
GDTEST_ANALYZER_ERROR
|
||||
The function signature doesn't match the parent. Parent signature is "int my_function(int)".
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
func test():
|
||||
print("Shouldn't reach this")
|
||||
|
||||
class Parent:
|
||||
func my_function() -> int:
|
||||
return 0
|
||||
|
||||
class Child extends Parent:
|
||||
func my_function() -> Vector2:
|
||||
return Vector2()
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
GDTEST_ANALYZER_ERROR
|
||||
The function signature doesn't match the parent. Parent signature is "int my_function()".
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
func test():
|
||||
var instance := Parent.new()
|
||||
var result := instance.my_function(1)
|
||||
print(result)
|
||||
assert(result == 1)
|
||||
instance = Child.new()
|
||||
result = instance.my_function(2)
|
||||
print(result)
|
||||
assert(result == 0)
|
||||
|
||||
class Parent:
|
||||
func my_function(par1: int) -> int:
|
||||
return par1
|
||||
|
||||
class Child extends Parent:
|
||||
func my_function(_par1: int, par2: int = 0) -> int:
|
||||
return par2
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
GDTEST_OK
|
||||
1
|
||||
0
|
||||
Reference in New Issue
Block a user