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

Allow match to use the continue keyword to allow for a switch-like continuation #9009

Open
greenfox1505 opened this issue Feb 1, 2024 · 5 comments
Labels
breaks compat Proposal will inevitably break compatibility topic:gdscript

Comments

@greenfox1505
Copy link

greenfox1505 commented Feb 1, 2024

Describe the project you are working on

Just a GDScript feature request.

Describe the problem or limitation you are having in your project

match is a replacement for switch from other langauges. It is a much easier syntax for most of what people use switch for. It however doesn't allow some niche functionality that exists in switch, like triggering multiple code blocks for a single switch/match.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

It allows a single match statement to have multiple effects without nesting match statements.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

func _ready():
	var x = {
		"name" : "Dennis",
		"age" : 45,
		"hair":"blue",
	}
	match x:
		{}:
			print("Empty dict")
		{"name": "Dennis"}:
			print("The name is Dennis")
		{"name": "Dennis", "age": var age,..}:
			print("Dennis is ", age, " years old.")
			continue #this would allow you to continue this match statement to the next check, similar to `switch`
		{"name": "Dennis", "hair": var color,..}:
			print("Dennis has ", color, " hair.")
		{"name", "age",..}:
			print("Has a name and an age, but it's not Dennis :(")
		{"key": "godotisawesome", ..}:
			print("I only checked for one entry and ignored the rest")

Currently godot only match the age value. But with a continue, he could match age and then continue to match color. It has the opposite effect of a break within switch. Or a similar effect as a continue in an for loop.

Without the continue line, the statement would output this:

Dennis is 45 years old.

With the continue line and langauge support, it could output:

Dennis is 45 years old.
Dennis has blue hair.

If this enhancement will not be used often, can it be worked around with a few lines of script?

It's a GDScript language feature. It can't easily be implemented with GDScript.

A similar effect would be difficult to manage without a nested match statement or similar control logic.

Is there a reason why this should be core and not an add-on in the asset library?

It's a GDScript language feature.

@Calinou Calinou changed the title Allow match to use the continue keyword to allow for a switch-like continuation. Allow match to use the continue keyword to allow for a switch-like continuation Feb 1, 2024
@greenfox1505
Copy link
Author

I flipped through some old proposals, and launched Godot3 again for the first time in a while. Looks like this WAS a feature in Godot3. The above code works as expected unchanged in Godot3.5.3.

@dalexeev
Copy link
Member

dalexeev commented Feb 2, 2024

If we want to add something like this, then it should be a separate keyword, not continue. fallthrough doesn't fit because it works differently in switch (executes the rest of the code without checking cases).

@dalexeev dalexeev added the breaks compat Proposal will inevitably break compatibility label Feb 2, 2024
@theraot
Copy link

theraot commented Feb 2, 2024

How about allowing another keyword to specify:

  • continue while
  • continue for
  • break while
  • break for

It might be useful in nseted loops, and continue match would not be an issue.

@greenfox1505
Copy link
Author

I think continue makes pretty good sense in this context. In a loop it does the next thing, and in this it also does the next thing. switch in other languages use break to leave the statement

Unless we find a really good alternative, IMHO, it would cause more confusion than it's worth. I'm really not a fan of two word solutions.

  • resume
  • fallthrough
  • continue

@dalexeev
Copy link
Member

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaks compat Proposal will inevitably break compatibility topic:gdscript
Projects
None yet
Development

No branches or pull requests

4 participants