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

Cannot call own functions inside a match pattern #18174

Closed
Bauxitedev opened this issue Apr 13, 2018 · 4 comments
Closed

Cannot call own functions inside a match pattern #18174

Bauxitedev opened this issue Apr 13, 2018 · 4 comments

Comments

@Bauxitedev
Copy link

Godot version:
3.0.2 Mono

OS/device including version:
KDE Neon

Issue description:
If you call a function you defined yourself inside of a pattern in a match clause, you get the following parser error:
built-in:8 - Parse Error: Invalid operator in pattern. Only index (A.B) is allowed

Additionally, most other methods don't seem to work either. But you can use math functions like pow, abs, etc. It could be that only "global" functions work inside a match clause?

Steps to reproduce:

  1. Create new project, add a node to the scene and assign this script to it:
extends Node

func add(a, b):
	return a + b
	
func _ready():
	match 4:
		add(2, 2):
			print("you win")
		_:
			print("you lose")
  1. I would expect "you win" to be printed, instead you get a parser error, as described above.
@vnen
Copy link
Member

vnen commented Apr 13, 2018

If you use math functions with literals, it'll be reduced to a constant by the parser, that's probably why it's allowed.

@karroffel
Copy link
Contributor

As @vnen said, constant expressions are reduced by the parser, but regular function calls can't. Also if you "match with a function call" then that function might have side effects, that's bad practise in general.

In most languages that have pattern matching you can only match on constant expressions, so this is actually expected.

One of the first iterations of the match statement allowed this, but as I said, as soon as you introduce side effect and all that, it becomes very messy, so I suggest we keep it as it is

@Bauxitedev
Copy link
Author

I see. It would be cool to see something like C++'s constexpr in GDScript, because then this could work.

@Calinou
Copy link
Member

Calinou commented May 26, 2020

Feature and improvement proposals for the Godot Engine are now being discussed and reviewed in a dedicated Godot Improvement Proposals (GIP) (godotengine/godot-proposals) issue tracker. The GIP tracker has a detailed issue template designed so that proposals include all the relevant information to start a productive discussion and help the community assess the validity of the proposal for the engine.

The main (godotengine/godot) tracker is now solely dedicated to bug reports and Pull Requests, enabling contributors to have a better focus on bug fixing work. Therefore, we are now closing all older feature proposals on the main issue tracker.

If you are interested in this feature proposal, please open a new proposal on the GIP tracker following the given issue template (after checking that it doesn't exist already). Be sure to reference this closed issue if it includes any relevant discussion (which you are also encouraged to summarize in the new proposal). Thanks in advance!

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

4 participants