1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

"none" keyword for optionals + more memory fixes

This commit is contained in:
Alexander Medvednikov
2019-09-17 22:41:58 +03:00
parent e40ab547ba
commit d1500511e6
12 changed files with 273 additions and 204 deletions

View File

@ -155,8 +155,7 @@ fn (m map) bs(query string, start, end int, out voidptr) {
fn preorder_keys(node &mapnode, keys mut []string, key_i int) int {
mut i := key_i
if !node.is_empty {
mut a := *keys
a[i] = node.key
keys[i] = node.key
i++
}
if !isnil(node.left) {

View File

@ -5,21 +5,26 @@
module builtin
struct Option {
data [255]byte
error string
ok bool
data [255]byte
error string
ok bool
is_none bool
}
// `fn foo() ?Foo { return foo }` => `fn foo() ?Foo { return opt_ok(foo); }`
fn opt_ok(data voidptr, size int) Option {
if size >= 255 {
panic('option size too big: $size (max is 255), this is a temporary limit')
}
res := Option {
panic('option size too big: $size (max is 255), this is a temporary limit')
}
res := Option {
ok: true
}
C.memcpy(res.data, data, size)
return res
C.memcpy(res.data, data, size)
return res
}
fn opt_none() Option {
return Option{ is_none: true }
}
pub fn error(s string) Option {