03 dicembre, 2019

Un nuovo linguaggio: V


It's a simple name that reflects the simplicity of the language, and it's easy to pronounce for everyone in the world.
Please note that the name of the language is "V", not "Vlang" or "V-Lang" etc.
Initially the language had the same name as the product it was created for: Volt. The extension was ".v", I didn't want to mess up git history, so I decided to name it V 🙂

V is a statically typed compiled programming language designed for building maintainable software. It's similar to Go and is also influenced by Oberon, Rust, Swift. Despite being simple, it gives a lot of power to the developer. Anything you can do in other languages, you can do in V.

fn main() {
    println('hello world')
}
... e via continua per una sintetica presentazione del linguaggio, con esempi eseguibili nel browser. Ah! è in fase di vfrenetico sviluppo, nella pagina dell'esecuzione online si trova la nota
The playground is running the latest version of V, updated multiple times per
day.
This is an alpha stage, please report all issues via GitHub.
Ci sono tante cose che promettono bene, mi piacciono; per esempio le variabili:

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


Variables are declared and initialized with :=. This is the only way to declare variables in V. This means that variables always have an initial value.

The variable's type is inferred from the value on the right hand side. To force a different type, use type conversion: the expression T(v) converts the value v to the type T.

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.


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


To change the value of the variable use =. In V, variables are immutable by default. To be able to change the value of the variable, you have to declare it with mut.

Please note the difference between := and =:= is used for declaring and initializing, = is used for assigning.


I tipi base:

bool
string
i8    i16  int  i64      i128 (soon)
byte  u16  u32  u64      u128 (soon)
rune // represents a Unicode code point
f32 f64
byteptr
voidptr

Le stringhe:

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"


In V, a string is a read-only array of bytes. String data is encoded using UTF-8. Strings are immutable.

Both single and double quotes can be used to denote strings. For consistency, vfmt converts double quotes to single quotes unless the string contains a single quote character.

Interpolation syntax is pretty simple. It also works with fields: 'age = $user.age'. If you need more complex expressions, use ${}: 'can register = ${user.age > 13}'.

All operators in V must have values of the same type on both sides. This code will *not* compile if age is an int:


println('age = ' + age)

We have to either convert age to a string:

println('age = ' + age.str())

or use string interpolation (preferred):

println('age = $age')

 To denote character literals, use `

a := `a`
assert 'aloha!'[0] == `a`


For raw strings, prepend r. Raw strings are not escaped:

s := r'hello\nworld'
println(s) // "hello\nworld"


OK, continua ma non adesso. Solo una parola sull'autore (di cui vorrei sapere di più): Alex Medvednikov.

Intanto V lo followo su Twitter (già da un po').
🔴

Nessun commento:

Posta un commento