V version: V 0.1.27 015d0c5
OS: Ubuntu 18.04
What did you do?
error: unexpected `name`, expecting `,`
1 | import sync
2 | fn test(wg *sync.WaitGroup) {
| ~~~~
3 |
4 | wg.done()
What did you expect to see?
clean execution
What did you see instead?
Error saying it is expecting a ',' instead of name (or type)
Note
Would be nice to have the equivalent of reflect.TypeOf for troubleshooting these issues, to verify type. In this case I am fairly certain it is a sync.WaitGroup pointer.
@havok4u pointers use & so it should be &sync.waitGroup. as for the v equivalent of reflect.TypeOf, there is typeof.
This is what I am defining:
fn test(wg * sync.WaitGroup) {
... Bunch of code....
}
fn main() {
wg := sync.new_waitgroup()
go test(&wg)
.....
}
so I am sending the address of "&" to the function which is then expecting a function, of which I get that message above. Hope that provides more clarity.
I modified my code to be
fn test(wg &sync.WaitGroup) {
... Bunch of code....
}
This fixes it, but isn't this very out of convention from any other language whom pass an address of a variable with "&" and receive it with a pointer symbol "*" in a function?
Ok we can close this, you don't put a &sync.WaitGroup in the passed argument, cause it is already a pointer. However you do have to establish that it is a mutable pointer and when you call it you have to say it is a mutable pointer. Seems odd you have to tell it it is mutable so many times, as at instantiation we already deem it a mutable pointer. Doing this the way I have it above,
fn test(wg &sync.WaitGroup) {
will cause your program to appear to work, but wg.active now equals 1342924544
So to fix it you have to do this:
fn test(mut wg sync.WaitGroup) {
defer { wg.done()}
for i := 0; i < 10; i++ {
println("test")
time.sleep_ms(1000)
}
return
}
fn main() {
wg := sync.new_waitgroup()
wg.add(1)
go test(mut wg) <----- Why do I have to define it a mutable yet again?
for i := 0; i < 8; i++ {
println("main")
time.sleep_ms(500)
}
wg.wait()
}
Actually one other catch. I do a
println(typeof(wg))
and I get sync.WaitGroup, but clearly what is returned according to the library and as witnessed in the above text it should be of type *sync.WaitGroup
If I run this in go code (go doesn't return a pointer for sync.WaitGroup, so I pass the address of wg to a function and print type) I get type *sync.WaitGroup
While troubleshooting the code, I missed that it was a pointer cause I did not get the correct type (i.e. pointer to a sync.WaitGroup) therefore I thought I had to pass the reference which caused some of the confusion here.
Long way of saying, we may need to make typeof a bit more advanced to show when it is actually a pointer of a type.
You did find a bug with mutable args, have to fix that.
&Foo is used for consistency with x := &Foo{} => (x &Foo) (x := Type{} => (x Type))
Most helpful comment
You did find a bug with mutable args, have to fix that.
&Foois used for consistency withx := &Foo{} => (x &Foo)(x := Type{}=>(x Type))