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

[GDScript] Cannot declare variable in lambda with the same name as parent scope #100573

Open
abstractionfactory opened this issue Dec 18, 2024 · 2 comments

Comments

@abstractionfactory
Copy link

Tested versions

Godot v4.3.stable - Windows 10.0.26100 - Vulkan (Forward+) - integrated Intel(R) Iris(R) Xe Graphics (Intel Corporation; 31.0.101.5595) - 13th Gen Intel(R) Core(TM) i5-1340P (16 Threads)

System information

N/A

Issue description

When creating a GDScript function containing a lambda in it, both with the same variable name, this will result in a compile error.

Steps to reproduce

Create the following script below. It will result in the following error:

Parse Error: There is already a variable named "result" declared in this scope.

Minimal reproduction project (MRP)

func test() -> void:
	var result := []
	var lambda := func () -> void:
		var result := []
@AThousandShips
Copy link
Member

Lambdas automatically allow you to bind and access variables in the parent context, I don't think this can be changed in a non-breaking way to allow you to both declare them and access them

@abstractionfactory
Copy link
Author

abstractionfactory commented Dec 18, 2024

@AThousandShips thank you, I know you can access variables from the parent scope, but this isn't a problem in any other language I have seen, they usually can bubble up the scope to the parent context if a variable is not found, similar to how you can access class variables from functions if they are not declared locally. Especially since lambda parameters can shadow parent scope variables just fine without problems:

func test() -> void:
	var foo := []
	var lambda := func (foo: String) -> void:
		pass

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants