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

all: refactor UnionSumType to SumType (#6944)

This commit is contained in:
Daniel Däschle
2020-11-25 12:09:40 +01:00
committed by GitHub
parent 96b73acad7
commit 7d6f97259f
37 changed files with 256 additions and 265 deletions

View File

@ -11,7 +11,7 @@ import v.util
import v.pref
// `Any` is a sum type that lists the possible types to be decoded and used.
pub __type Any = string | int | i64 | f32 | f64 | any_int | any_float | bool | Null | []Any | map[string]Any
pub type Any = string | int | i64 | f32 | f64 | any_int | any_float | bool | Null | []Any | map[string]Any
// `Null` struct is a simple representation of the `null` value in JSON.
pub struct Null {}
@ -287,7 +287,7 @@ fn (mut p Parser) decode_string() ?Any {
continue
}
else { return error('invalid backslash escape.') }
}
}
if int(peek) == 85 {
return error('unicode endpoints must be in lowercase `u`.')

View File

@ -7,9 +7,9 @@ import strings
fn write_value(v Any, i int, len int, mut wr strings.Builder) {
str := v.str()
if v is string {
wr.write('"$str"')
} else {
if v is string {
wr.write('"$str"')
} else {
wr.write(str)
}
if i >= len-1 { return }
@ -51,7 +51,7 @@ pub fn (flds []Any) str() string {
// String representation of the `Any` type.
pub fn (f Any) str() string {
match union f {
match f {
string { return f }
int { return f.str() }
i64 { return f.str() }

View File

@ -79,7 +79,7 @@ pub fn (f Any) as_map() map[string]Any {
// Use `Any` as an integer.
pub fn (f Any) int() int {
match union f {
match f {
int { return f }
i64 { return int(f) }
f64 { return f.str().int() }
@ -91,7 +91,7 @@ pub fn (f Any) int() int {
// Use `Any` as a 64-bit integer.
pub fn (f Any) i64() i64 {
match union f {
match f {
int { return f }
i64 { return int(f) }
f64 { return f.str().i64() }
@ -103,7 +103,7 @@ pub fn (f Any) i64() i64 {
// Use `Any` as a 32-bit float.
pub fn (f Any) f32() f32 {
match union f {
match f {
int { return f }
i64 { return f.str().f32() }
f64 { return f.str().f32() }
@ -114,7 +114,7 @@ pub fn (f Any) f32() f32 {
// Use `Any` as a float.
pub fn (f Any) f64() f64 {
match union f {
match f {
int { return f }
i64 { return f }
f64 { return f }
@ -138,7 +138,7 @@ pub fn (f Any) arr() []Any {
// Use `Any` as a bool
pub fn (f Any) bool() bool {
match union f {
match f {
bool { return f }
string { return f.bool() }
else { return false }