143 lines
3.6 KiB
Plaintext
143 lines
3.6 KiB
Plaintext
;; A T where a T? is wanted is Some of it (decision 138): each position,
|
|
;; a literal built at the payload's type, nested Options, generics, and the
|
|
;; arms of an if, a match and a kept chain.
|
|
|
|
struct P
|
|
a: i64?
|
|
b: i32?
|
|
|
|
fn show(o: i32?) -> i32 = o ?? -9
|
|
|
|
fn back(b: bool, x: i32) -> i32?
|
|
if b
|
|
return x
|
|
None
|
|
|
|
fn pick(b: bool, x: i32) -> i32?
|
|
if b then x else None
|
|
|
|
fn pick2(b: bool, x: i32) -> i32?
|
|
if b then None else x
|
|
|
|
fn two(o: Option(i32?)) -> str
|
|
match o
|
|
Some(i) -> if i? then "some some" else "some none"
|
|
None -> "none"
|
|
|
|
fn first(o: $u?) -> $u = o!
|
|
|
|
fn wrap(x: $t) -> $t? = x
|
|
|
|
;; A generic body's own $t handed to a $u? parameter.
|
|
fn through(x: $t) -> $t = first(x)
|
|
|
|
;; A None arm first takes its type from the arms after it.
|
|
fn arms(k: i32, x: i32) -> i32?
|
|
match k
|
|
5 -> None
|
|
4 -> x
|
|
_ -> 0
|
|
|
|
fn big(x: i64?) -> i64 = x ?? 0
|
|
|
|
;; A T?? is wanted, so each arm is Some of its value and the chain's None is
|
|
;; the outer one.
|
|
fn chain(a: bool, b: bool, opt: i32?) -> Option(i32?)
|
|
if a
|
|
1
|
|
elif b
|
|
opt
|
|
|
|
;; Nothing wanted: a T arm beside a T? arm makes the chain a T?, flattened
|
|
;; one level (decision 140).
|
|
fn flat(a: bool, b: bool, opt: i32?) -> i32?
|
|
let r =
|
|
if a
|
|
1
|
|
elif b
|
|
opt
|
|
r
|
|
|
|
fn main()
|
|
;; assignment, and a literal at the payload's width
|
|
let s: i64? = None
|
|
s = -1
|
|
println(s ?? 0)
|
|
;; a let with an annotation, from a literal, a name and arithmetic
|
|
let x: i32 = 4
|
|
let a: i32? = x + 1
|
|
let w: i64? = x
|
|
let f: f64? = 2
|
|
println(a ?? 0, w ?? 0, f ?? 0.0)
|
|
;; an argument and a return value
|
|
println(show(7), back(true, 8) ?? -1, back(false, 8) ?? -1)
|
|
;; a struct field
|
|
let p = P{.a 3 .b x}
|
|
println(p.a ?? 0, p.b ?? 0)
|
|
p.b = 7
|
|
println(p.b ?? 0)
|
|
;; an array and a Vec element
|
|
let xs: [3 i32?] = [1, None, x]
|
|
println(xs[0] ?? 0, xs[1] ?? 0, xs[2] ?? 0)
|
|
xs[1] = 5
|
|
println(xs[1] ?? 0)
|
|
let v: Vec(i32?) = vec-new(i32?)
|
|
push(v, 6)
|
|
push(v, None)
|
|
println(length(v), v[0] ?? 0, v[1] ?? 0)
|
|
;; the arms of an if and a match
|
|
println(pick(true, 2) ?? -1, pick(false, 2) ?? -1, pick2(true, 2) ?? -1, pick2(false, 2) ?? -1)
|
|
let m = match x
|
|
4 -> x
|
|
_ -> None
|
|
println(m ?? 0)
|
|
println(arms(5, 3) ?? -1, arms(4, 3) ?? -1, arms(1, 3) ?? -1)
|
|
;; nested: a T into a T?? is Some(Some(t)), a T? is Some of it
|
|
let nn: Option(i32?) = 5
|
|
let none: i32? = None
|
|
let nn2: Option(i32?) = none
|
|
println(two(nn), two(nn2))
|
|
;; kept chains over T and T? arms
|
|
println(two(chain(true, false, none)), two(chain(false, true, none)), two(chain(false, false, none)))
|
|
println(flat(true, false, none) ?? -1, flat(false, true, Some(6)) ?? -1, flat(false, true, none) ?? -1, flat(false, false, none) ?? -1)
|
|
let kk =
|
|
if x > 9
|
|
none
|
|
elif x > 1
|
|
1
|
|
println(kk ?? -1)
|
|
;; generics
|
|
println(first(9), first(Some(8)), wrap(3) ?? 0, through(5))
|
|
;; a literal local takes the payload's type from an Option use, as it
|
|
;; takes T from a T use: each pair prints the same
|
|
let la = 4
|
|
let wa: i64? = la
|
|
let lb = 4
|
|
let wb: i64 = lb
|
|
println(la * 1000000000, lb * 1000000000, wa ?? 0, wb)
|
|
let lc = 4
|
|
println(big(lc), lc * 1000000000)
|
|
let lf = 7
|
|
let wf: f64? = lf
|
|
let lg = 7
|
|
let wg: f64 = lg
|
|
println(lf / 2, lg / 2, wf ?? 0.0, wg)
|
|
let lu = 200
|
|
let wu: u8? = lu
|
|
let lv = 200
|
|
let wv: u8 = lv
|
|
println(wu ?? 0, wv)
|
|
;; None first in an if, as in a match
|
|
let e1 = if x > 1 then None else 5
|
|
let e2 = if x > 1 then 5 else None
|
|
println(e1 ?? -1, e2 ?? -1)
|
|
;; an Option around an array of Options
|
|
let ao: [2 i32?]? = [1, None]
|
|
let ai = ao!
|
|
println(ai[0] ?? 0, ai[1] ?? 0)
|
|
;; a narrowed name still takes a payload value
|
|
let o: i32? = Some(1)
|
|
if o?
|
|
o = 10
|
|
println(o)
|