Comments (3)
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.
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.
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)
- seems error happend when trying to access a folder under Documents macos HOT 2
- v test does not print float value HOT 2
- `v.reflection` `attrs` rework HOT 2
- segmentation fault when trying to use an array return type for a pool_processor callback
- calculated const causes failure when used as fixed array size HOT 3
- Error C (from H file to V : warning: implicit declaration of function 'sha3_finalize' /tmp/v_1000/SHA3.01HYG2REZ8HW9F433E3J5B37XR.tmp.c:13117: error: '{' expected (got ";") HOT 1
- examples/sokol/02_cube_glsl VALIDATE_ABND_FS_EXPECTED_SAMPLER_BINDING
- gx - operator overloading not working for Color HOT 2
- Macos uses db.mysql,C error found HOT 8
- V compiler error when adding error processing using or {<expression>} HOT 1
- Is the data of an option reference returned from a method changed? HOT 1
- Export to shared library HOT 1
- An enum with attribute @[flag] does not accept a value with a mixture of enumerations.
- gdextension HOT 2
- JSX support in vweb
- Add statically compiled tiny_gcc support HOT 11
- veb automatically generate RESTful API documentation with Swagger
- V upgrade and downgrade functions HOT 1
- Support placing API files in different folders HOT 12
- Can not copy from the code block for V HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from v.