V language preview
1. Structs and methods
structs, functions, and string interpolation
horizon-dark
module main
struct Point {
mut:
x f64
y f64
}
fn (p Point) distance() f64 {
return p.x * p.x + p.y * p.y
}
fn main() {
p := Point{x: 3.0, y: 4.0}
println("distance = ${p.distance()}")
} atom-one-dark
module main
struct Point {
mut:
x f64
y f64
}
fn (p Point) distance() f64 {
return p.x * p.x + p.y * p.y
}
fn main() {
p := Point{x: 3.0, y: 4.0}
println("distance = ${p.distance()}")
} github-dark
module main
struct Point {
mut:
x f64
y f64
}
fn (p Point) distance() f64 {
return p.x * p.x + p.y * p.y
}
fn main() {
p := Point{x: 3.0, y: 4.0}
println("distance = ${p.distance()}")
} dracula
module main
struct Point {
mut:
x f64
y f64
}
fn (p Point) distance() f64 {
return p.x * p.x + p.y * p.y
}
fn main() {
p := Point{x: 3.0, y: 4.0}
println("distance = ${p.distance()}")
} nord
module main
struct Point {
mut:
x f64
y f64
}
fn (p Point) distance() f64 {
return p.x * p.x + p.y * p.y
}
fn main() {
p := Point{x: 3.0, y: 4.0}
println("distance = ${p.distance()}")
} github
module main
struct Point {
mut:
x f64
y f64
}
fn (p Point) distance() f64 {
return p.x * p.x + p.y * p.y
}
fn main() {
p := Point{x: 3.0, y: 4.0}
println("distance = ${p.distance()}")
} 2. Options and matching
option types, match, and enums
horizon-dark
enum Color {
red
green
blue
}
fn parse(s string) ?Color {
return match s {
"red" { Color.red }
"green" { Color.green }
else { none }
}
} atom-one-dark
enum Color {
red
green
blue
}
fn parse(s string) ?Color {
return match s {
"red" { Color.red }
"green" { Color.green }
else { none }
}
} github-dark
enum Color {
red
green
blue
}
fn parse(s string) ?Color {
return match s {
"red" { Color.red }
"green" { Color.green }
else { none }
}
} dracula
enum Color {
red
green
blue
}
fn parse(s string) ?Color {
return match s {
"red" { Color.red }
"green" { Color.green }
else { none }
}
} nord
enum Color {
red
green
blue
}
fn parse(s string) ?Color {
return match s {
"red" { Color.red }
"green" { Color.green }
else { none }
}
} github
enum Color {
red
green
blue
}
fn parse(s string) ?Color {
return match s {
"red" { Color.red }
"green" { Color.green }
else { none }
}
} 3. Comptime keywords and raw strings
$if/$else and non-interpolating r'...' strings
horizon-dark
fn describe() string {
$if windows {
return 'windows build'
} $else {
return 'other platform'
}
}
fn literal_path() string {
// raw string: ${name} is NOT interpolated here
return r'C:\Users\${name}'
} atom-one-dark
fn describe() string {
$if windows {
return 'windows build'
} $else {
return 'other platform'
}
}
fn literal_path() string {
// raw string: ${name} is NOT interpolated here
return r'C:\Users\${name}'
} github-dark
fn describe() string {
$if windows {
return 'windows build'
} $else {
return 'other platform'
}
}
fn literal_path() string {
// raw string: ${name} is NOT interpolated here
return r'C:\Users\${name}'
} dracula
fn describe() string {
$if windows {
return 'windows build'
} $else {
return 'other platform'
}
}
fn literal_path() string {
// raw string: ${name} is NOT interpolated here
return r'C:\Users\${name}'
} nord
fn describe() string {
$if windows {
return 'windows build'
} $else {
return 'other platform'
}
}
fn literal_path() string {
// raw string: ${name} is NOT interpolated here
return r'C:\Users\${name}'
} github
fn describe() string {
$if windows {
return 'windows build'
} $else {
return 'other platform'
}
}
fn literal_path() string {
// raw string: ${name} is NOT interpolated here
return r'C:\Users\${name}'
}