Initial project import
This commit is contained in:
commit
23f2d29e32
11957 files changed
+1754577
No files matched your search
@@ -0,0 +1,56 @@
|
||||
// This example shows how to use V's compile time defines and values via the -d flag and $d() function.
|
||||
//
|
||||
// To change the default values below, pass compile flags to v using: `-d ident=<value>`
|
||||
// Examples:
|
||||
// ´´´
|
||||
// v -d header -d pad=10 -d jobs=8 run d_compile_value.v
|
||||
// v -d id="Bob" -d pad=2 -d pad_char='$' -d jobs=10 run d_compile_value.v
|
||||
// ```
|
||||
|
||||
const pad = $d('pad', 5)
|
||||
const pad_char = $d('pad_char', `-`)
|
||||
|
||||
const footer = '
|
||||
Available compile time flags:
|
||||
-d pad=<i64>
|
||||
-d pad_char=<character>
|
||||
-d id="<string>"
|
||||
-d jobs=<i64>
|
||||
-d header=<bool>
|
||||
You can turn this message off with:
|
||||
-d footer=false'
|
||||
|
||||
struct Job {
|
||||
id string = $d('id', 'Job') // adjust with `-d id="My ID"`
|
||||
}
|
||||
|
||||
struct App {
|
||||
jobs [$d('jobs', 4)]Job // adjust fixed array size with `-d jobs=6`
|
||||
}
|
||||
|
||||
// println_padded prints `str` to stdout with a padding.
|
||||
// Padding length and padding character can be adjusted at compile time
|
||||
// via `-d pad=1` and `-d pad_char=x`.
|
||||
fn println_padded(str string) {
|
||||
for _ in 0 .. pad {
|
||||
print(rune(pad_char))
|
||||
}
|
||||
println(' ${str}')
|
||||
}
|
||||
|
||||
fn main() {
|
||||
header := $d('header', false) // adjust header variable with `-d header=true` or simply `-d header`
|
||||
if header {
|
||||
println_padded('Compile Value Example')
|
||||
}
|
||||
|
||||
app := App{}
|
||||
for i, job in app.jobs {
|
||||
println_padded('${job.id} ${i + 1}')
|
||||
}
|
||||
|
||||
// Turn the footer message off using `-d footer=false`
|
||||
if $d('footer', true) {
|
||||
println(footer)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
struct App {}
|
||||
|
||||
fn (mut app App) method_one() {}
|
||||
|
||||
fn (mut app App) method_two() int {
|
||||
return 0
|
||||
}
|
||||
|
||||
fn (mut app App) method_three(s string) string {
|
||||
return s
|
||||
}
|
||||
|
||||
fn main() {
|
||||
$for method in App.methods {
|
||||
$if method.typ is fn (string) string {
|
||||
println('${method.name} IS `fn(string) string`')
|
||||
} $else {
|
||||
println('${method.name} is NOT `fn(string) string`')
|
||||
}
|
||||
$if method.return_type !is int {
|
||||
println('${method.name} does NOT return `int`')
|
||||
} $else {
|
||||
println('${method.name} DOES return `int`')
|
||||
}
|
||||
$if method.args[0].typ !is string {
|
||||
println("${method.name}'s first arg is NOT `string`")
|
||||
} $else {
|
||||
println("${method.name}'s first arg IS `string`")
|
||||
}
|
||||
// TODO: Double inversion, should this even be allowed?
|
||||
$if method.typ is fn () {
|
||||
println('${method.name} IS a void method')
|
||||
} $else {
|
||||
println('${method.name} is NOT a void method')
|
||||
}
|
||||
println('')
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
// An example deserializer implementation
|
||||
|
||||
struct User {
|
||||
name string
|
||||
age int
|
||||
}
|
||||
|
||||
fn main() {
|
||||
data := 'name=Alice\nage=18'
|
||||
user := decode[User](data)
|
||||
println(user)
|
||||
}
|
||||
|
||||
fn decode[T](data string) T {
|
||||
mut result := T{}
|
||||
// compile-time `for` loop
|
||||
// T.fields gives an array of a field metadata type
|
||||
$for field in T.fields {
|
||||
$if field.typ is string {
|
||||
// $(string_expr) produces an identifier
|
||||
result.$(field.name) = get_string(data, field.name)
|
||||
} $else $if field.typ is int {
|
||||
result.$(field.name) = get_int(data, field.name)
|
||||
}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
fn get_string(data string, field_name string) string {
|
||||
for line in data.split_into_lines() {
|
||||
key_val := line.split('=')
|
||||
if key_val[0] == field_name {
|
||||
return key_val[1]
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
fn get_int(data string, field string) int {
|
||||
return get_string(data, field).int()
|
||||
}
|
||||
|
||||
// `decode<User>` generates:
|
||||
// fn decode_User(data string) User {
|
||||
// mut result := User{}
|
||||
// result.name = get_string(data, 'name')
|
||||
// result.age = get_int(data, 'age')
|
||||
// return result
|
||||
// }
|
||||
Reference in new issue
Block a user