Giter Site home page Giter Site logo

Comments (3)

JalonSolov avatar JalonSolov commented on June 3, 2024

Your code does not match the docs, since you have wrapped most of it inside a fn main.

The next() function must be outside main, as it's own function, for this to work.

If you look more closely at the docs, you'll see there's no main at all - it is not needed, as this is an example of "script mode" V.

from v.

ookami-001 avatar ookami-001 commented on June 3, 2024

You are right. After I changed the contents of the main.v file to the following:

module main

struct SquareIterator {
	arr []int
mut:
	idx int
}

fn (mut iter SquareIterator) next() ?int {
	if iter.idx >= iter.arr.len {
		return none
	}
	defer {
		iter.idx++
	}
	return iter.arr[iter.idx] * iter.arr[iter.idx]
}

fn main() {

	nums := [1, 2, 3, 4, 5]
	
	iter := SquareIterator{
		arr: nums
	}

	for squared in iter {
		println(squared)
	}
	
}

the code compiled successfully and produced the expected outcome:

1
4
9
16
25

Still, I feel that this will catch many new learners off guard as many will probably do exactly as I did by including the next() function inside the main() function. I strongly think that example in the documentation needs to be edited to look like the code I posted above to avoid potential confusion from new learners. The more people who fall in love with V the better and clearer documentation is one way of achieving that.

I will look out for more issues like this as I continue my learning of V.

from v.

JalonSolov avatar JalonSolov commented on June 3, 2024

The proper edit to add the main function would be

module main

struct SquareIterator {
	arr []int
mut:
	idx int
}

fn (mut iter SquareIterator) next() ?int {
	if iter.idx >= iter.arr.len {
		return none
	}
	defer {
		iter.idx++
	}
	return iter.arr[iter.idx] * iter.arr[iter.idx]
}

fn main() {
	nums := [1, 2, 3, 4, 5]
	
	iter := SquareIterator{
		arr: nums
	}

	for squared in iter {
		println(squared)
	}
}

as V doesn't allow you to wrap a named function inside another function (hence the error message you received):

error: unexpected token `(` after anonymous function signature, expecting `{`

Only anonymous functions can be declared inside other functions.

It's a toss-up whether this would make a difference... may people would likely try running the example as-is, rather than adding to it, just to see what it would do.

from v.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.