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

Custom iterator struct constructor does not work with for x in Iterator{} { #21179

Closed
spytheman opened this issue Apr 3, 2024 · 0 comments · Fixed by #21282
Closed

Custom iterator struct constructor does not work with for x in Iterator{} { #21179

spytheman opened this issue Apr 3, 2024 · 0 comments · Fixed by #21282
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).

Comments

@spytheman
Copy link
Member

spytheman commented Apr 3, 2024

V doctor:

V full version: V 0.4.5 872bcbc.55cafa9
OS: linux, Ubuntu 20.04.6 LTS
Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i3-3225 CPU @ 3.30GHz

getwd: /space/v/oo
vexe: /space/v/oo/v
vexe mtime: 2024-04-03 14:54:06

vroot: OK, value: /space/v/oo
VMODULES: OK, value: /home/delian/.vmodules
VTMP: OK, value: /tmp/v_1000

Git version: git version 2.42.0
Git vroot status: weekly.2024.14-8-g1dd1b650 (2 commit(s) behind V master)
.git/config present: true

CC version: cc (Ubuntu 10.5.0-1ubuntu1~20.04) 10.5.0
thirdparty/tcc status: thirdparty-linux-amd64 40e5cbb5

What did you do?
v -g -o vdbg cmd/v && vdbg /v/misc/2024_04_03__18/a.v

struct Iterator {
mut:
	counter int
}

fn (mut iter Iterator) next() ?int {
	if iter.counter > 0 {
		iter.counter--
		return iter.counter
	}
	return none
}

for x in Iterator{counter: 10} {
	dump(x)
}

What did you expect to see?

a compiled program, that when run, prints the numbers from 9 to 0 in descending order

What did you see instead?

/space/v/misc/2024_04_03__18/a.v:14:5: warning: unused variable: `x`
   12 | }
   13 | 
   14 | for x in Iterator{counter: 10} {
      |     ^
   15 |     dump(x)
   16 | }
/space/v/misc/2024_04_03__18/a.v:14:19: warning: label `counter` defined and not used
   12 | }
   13 | 
   14 | for x in Iterator{counter: 10} {
      |                   ~~~~~~~~~~~
   15 |     dump(x)
   16 | }
/space/v/misc/2024_04_03__18/a.v:14:10: error: undefined ident: `Iterator`
   12 | }
   13 | 
   14 | for x in Iterator{counter: 10} {
      |          ~~~~~~~~
   15 |     dump(x)
   16 | }
/space/v/misc/2024_04_03__18/a.v:15:7: error: undefined ident: `x`
   13 | 
   14 | for x in Iterator{counter: 10} {
   15 |     dump(x)
      |          ^
   16 | }
/space/v/misc/2024_04_03__18/a.v:15:7: error: dump expression can not be void
   13 | 
   14 | for x in Iterator{counter: 10} {
   15 |     dump(x)
      |          ^
   16 | }

Note that doing this instead, works:

...
set := Iterator{counter: 10}
for x in set {
...

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@spytheman spytheman added Bug This tag is applied to issues which reports bugs. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones). Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. labels Apr 3, 2024
@felipensp felipensp self-assigned this Apr 14, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: Parser Bugs/feature requests, that are related to the V parser or syntax (*not* semantic ones).
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants