V: Variable example didn't work on playground

Created on 16 Jan 2020  路  13Comments  路  Source: vlang/v

V version: not sure
OS: not sure

What did you do?
run example on Variable section

name := 'Bob'
age := 20
large_number := i64(9999999999)
println(name)
println(age)
println(large_number)

What did you expect to see?
compiled correctly

What did you see instead?

cannot use type `void` as type `string` in first argument to `println()`
    2| age := 20
Bug Playground Documentation

All 13 comments

next example also error:

mut age := 20
println(age)
age = 21
println(age)

the error:

cannot use type `void` as type `string` in first argument to `println()`
    1| mut age := 20

The Strings section also error:

name := 'Bob'
println('Hello, $name!')  // `$` is used for string interpolation
println(name.len)

bobby := name + 'by' // + is used to concatenate strings
println(bobby) // "Bobby" 

println(bobby[1..3]) // "ob" 
mut s := 'hello '
s += 'world' // `+=` is used to append to a string
println(s) // "hello world" 

the error:

cannot interpolate this value
    1| name := 'Bob'

The Array section also error:

mut nums := [1, 2, 3]
println(nums) // "[1, 2, 3]"
println(nums[1]) // "2"

nums << 4
println(nums) // "[1, 2, 3, 4]"

nums << [5, 6, 7]
println(nums) // "[1, 2, 3, 4, 5, 6, 7]"

mut names := ['John']
names << 'Peter'
names << 'Sam'
// names << 10  <-- This will not compile. `names` is an array of strings.
println(names.len) // "3"
println('Alex' in names) // "false"

names = [] // The array is now empty

// We can also preallocate a certain amount of elements.
ids := [0].repeat(50) // This creates an array with 50 zeros

the error:

cannot use type `void` as type `string` in first argument to `println()`
    1| mut nums := [1, 2, 3]

The Maps section also error:

mut m := map[string]int // Only maps with string keys are allowed for now 
m['one'] = 1
m['two'] = 2
println(m['one']) // "1" 
println(m['bad_key']) // "0" 
println('bad_key' in m) // Use `in` to detect whether such key exists
m.delete('two')

numbers := {
    'one': 1,
    'two': 2,
}

the error:

syntax error: unexpected `'one'`, expecting `name`
    1| mut m := map[string]int // Only maps with string keys are allowed for now 

The If section also error:

a := 10
b := 20
if a < b {
    println('$a < $b')
} else if a > b {
    println('$a > $b')
} else {
    println('$a == $b')
}

the error:

cannot interpolate this value
    2| b := 20

but it's ok when wrapped with fn main()

V version: not sure
OS: not sure

What did you do?
run example on Variable section

name := 'Bob'
age := 20
large_number := i64(9999999999)
println(name)
println(age)
println(large_number)

What did you expect to see?
compiled correctly

What did you see instead?

cannot use type `void` as type `string` in first argument to `println()`
    2| age := 20

Appreciate if you could enclose the code within the main block

module main

fn main() {
    name := 'Bob'
    age := 20
    large_number := i64(9999999999999999)

    println(name)
    println(age.str())
    println(large_number.str()) 
}

this should work, i tested other examples as well seems works fine for me.

The tutorial suggests, at the very beginning:

fn main() declaration can be skipped in one file programs. This is useful when writing small programs, "scripts", or just learning the language. For brevity, fn main() will be skipped in this tutorial.

I guess it should be updated then since following this suggestion appears to break lots of examples.

This code (extracted from the documentation) also appears to produce an error:

fn main() {
  numbers := {
    'one': 1,
    'two': 2,
  }
}

next token = }
test.v:14:1: syntax error: unexpected }, expecting STR
12| 'two': 2,
13| }
14| }

This issue probably has to do with this from the docs:

Unlike most other languages, V only allows defining variables in functions. Global (module level) variables are not allowed. There's no global state in V.

Nonetheless, I also think the examples should be updated to reflect that

@warpdesign , this is a regression bug in v:

fn main() {
  numbers := {
    'one': 1,
    'two': 2,
  }
}

It will be fixed soon (it errors because of the trailing , after 2).

Just a reminder: github issues work best, when they are focused on a single problem.
If you find another problem, even if it is in the same general area, it is better to have a new issue opened, so that we can track it more easily, and close it when a solution is found.

Most of samples now works fine on "V 0.1.25 876b73f"

name := 'Bob'
age := 20
large_number := i64(9999999999)
println(name)
println(age)
println(large_number)
mut age := 20
println(age)
age = 21
println(age)
name := 'Bob'
println('Hello, $name!')  // `$` is used for string interpolation
println(name.len)

bobby := name + 'by' // + is used to concatenate strings
println(bobby) // "Bobby" 

println(bobby[1..3]) // "ob" 
mut s := 'hello '
s += 'world' // `+=` is used to append to a string
println(s) // "hello world" 
mut nums := [1, 2, 3]
println(nums) // "[1, 2, 3]"
println(nums[1]) // "2"

nums << 4
println(nums) // "[1, 2, 3, 4]"

nums << [5, 6, 7]
println(nums) // "[1, 2, 3, 4, 5, 6, 7]"

mut names := ['John']
names << 'Peter'
names << 'Sam'
// names << 10  <-- This will not compile. `names` is an array of strings.
println(names.len) // "3"
println('Alex' in names) // "false"

names = [] // The array is now empty

// We can also preallocate a certain amount of elements.
ids := [0].repeat(50) // This creates an array with 50 zeros
a := 10
b := 20
if a < b {
    println('$a < $b')
} else if a > b {
    println('$a > $b')
} else {
    println('$a == $b')
}

The following Map example still causes an error:

mut m := map[string]int // Only maps with string keys are allowed for now 
m['one'] = 1
m['two'] = 2
println(m['one']) // "1" 
println(m['bad_key']) // "0" 
println('bad_key' in m) // Use `in` to detect whether such key exists
m.delete('two')

numbers := {
    'one': 1,
    'two': 2,
}

next token = ]
test.v:2:8: syntax error: unexpected 'one', expecting name
1| mut m := map[string]int // Only maps with string keys are allowed for now
2| m['one'] = 1
^

All of the cited examples work fine in the current playground.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lobotony picture lobotony  路  3Comments

shouji-kazuo picture shouji-kazuo  路  3Comments

radare picture radare  路  3Comments

taojy123 picture taojy123  路  3Comments

XVilka picture XVilka  路  3Comments