Files

180 lines
3.1 KiB
Plaintext

BasicError :: enum {
bad
worse
}
DetailError :: union(enum) {
code i32
info struct {
code i32
extra i32
}
empty void
}
BasicOrDetail :: alias BasicError | DetailError
Left :: enum {
left
}
Right :: enum {
right
}
Both :: alias Left | Right
BoxA :: union(enum) {
a i32
}
BoxB :: union(enum) {
b void
}
Box :: alias BoxA | BoxB
maybe func(value i32) i32 ! BasicError {
if (value == 0) return .bad
if (value < 0) return .worse
return value + 1
}
via_try func(value i32) i32 ! BasicError {
unwrapped :: try maybe(value)
return unwrapped + 1
}
with_detail func(value i32) i32 ! DetailError {
if (value == 0) return .code{5}
if (value == 1) return .empty
if (value == 3) return .info{code = 8, extra = 9}
return value
}
via_widen func(value i32) i32 ! BasicOrDetail {
unwrapped :: try maybe(value)
return unwrapped + 2
}
catch_basic func(value i32) i32 {
return maybe(value) catch |e| {
result i32 :: if (e == .bad) {
yield 21
} else {
yield 22
}
yield result
}
}
catch_detail func(value i32) i32 {
return with_detail(value) catch |e| {
result i32 :: match e {
.code |n|: n + 30
.info |info|: info.code + info.extra + 30
.empty: 40
}
yield result
}
}
catch_widen func(value i32) i32 {
return via_widen(value) catch |e| {
result i32 :: match e {
.bad: 50
.worse: 51
.code |n|: n
.info |info|: info.code + info.extra
.empty: 52
}
yield result
}
}
pick func(value Both) i32 {
match value {
.left: return 10
.right: return 20
}
}
payload func(value Box) i32 {
match value {
.a |n|: return n
.b: return 3
}
}
inline_enum func(value i32) i32 ! enum {
inline_bad
inline_worse
} {
if (value == 0) return .inline_bad
if (value < 0) return .inline_worse
return value
}
inline_detail func(value i32) i32 ! union(enum) {
inline_code i32
inline_empty void
} {
if (value == 0) return .inline_code{6}
if (value == 1) return .inline_empty
return value
}
main func() i32 {
acc i32 := 0
a :: maybe(0) catch 7
b :: maybe(4) catch 99
c :: via_try(3) catch 99
d :: via_try(0) catch 11
e :: with_detail(0) catch 13
f :: with_detail(2) catch 99
g :: catch_basic(0)
h :: catch_basic(-1)
i :: catch_detail(0)
j :: catch_detail(1)
k :: via_widen(3) catch 99
l :: catch_widen(0)
m :: catch_widen(-1)
n :: inline_enum(0) catch |e| {
result i32 :: if (e == .inline_bad) {
yield 60
} else {
yield 61
}
yield result
}
o :: inline_detail(0) catch |e| {
result i32 :: match e {
.inline_code |value|: value + 70
.inline_empty: 80
}
yield result
}
p :: with_detail(3) catch |e| {
result i32 :: match e {
.code |value|: value
.info |info|: info.code + info.extra
.empty: 0
}
yield result
}
acc = acc + a + b + c + d + e + f + g + h + i + j + k + l + m + n + o + p
acc = acc + pick(.left)
r Right := .right
acc = acc + pick(r)
box BoxA := .a{8}
acc = acc + payload(box)
empty BoxB := .b
acc = acc + payload(empty)
acc = acc + payload(.a{9})
# 7 + 5 + 5 + 11 + 13 + 2 + 21 + 22 + 35 + 40 + 6 + 50 + 51 + 60 + 76 + 17 + 10 + 20 + 8 + 3 + 9 = 471
return acc - 471
}