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

Recognition of _ in Initial Condition of for Loop #1461

Open
notJoon opened this issue Dec 19, 2023 · 4 comments
Open

Recognition of _ in Initial Condition of for Loop #1461

notJoon opened this issue Dec 19, 2023 · 4 comments

Comments

@notJoon
Copy link
Member

notJoon commented Dec 19, 2023

Description

I'm not sure if this is a bug or a feature. In gno, using _ (underscore, typically for ignoring values) in the initialization of a for loop is accepted. Such usage is not permitted in Go syntax.

package main

func main() {
	i := 1
	for _; i < 5; i++ {
		println(i)
	}
}

// output: 1 2 3 4
// expect: cannot use _ as value or type

Note that, just like in standard Go, gno also allows leaving the initialization part of a for loop blank, as shown below:

package main

func main() {
	i := 1
	for ; i < 5; i++ {
		println(i)
	}
}

// output: 
1 
2 
3
4 

May related with op_exec

@notJoon
Copy link
Member Author

notJoon commented Dec 19, 2023

This test reproduces the case where the loop's initialization condition is underscored, as shown below:

func TestForLoopIgnoredInital(t *testing.T) {
	m := NewMachine("test", nil)
	main := FuncD("main", nil, nil, Ss(
		A("i", ":=", "0"),
		For(
			X("_"),
			X("i < 5"),
			Inc("i"),
		),
	))
	m.RunDeclaration(main)
	m.RunMain()
}

However, I'm not sure how to test for an empty value in the initial condition.

@thehowl
Copy link
Member

thehowl commented Dec 19, 2023

It probably shouldn't accept the syntax and have the same behaviour as Go, there's no good reason why the feature should be accepted in Gno. However, I think it's safe to say that if the only statement accepted in this context _ this can be considered low-priority :)

@notJoon
Copy link
Member Author

notJoon commented Dec 20, 2023

@thehowl I didn't think there was anything wrong with it, but it was vague. Thanks for the feedback

@notJoon notJoon closed this as completed Dec 20, 2023
@thehowl thehowl reopened this Dec 20, 2023
@thehowl
Copy link
Member

thehowl commented Dec 20, 2023

still an issue we should fix (I think more likely by not accepting all expressions as statements)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🔵 Not Needed for Launch
Development

No branches or pull requests

2 participants