flan/test/programs/autowrap.fln

86 lines
2.1 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
fn chain(a: bool, b: bool, opt: i32?) -> Option(i32?)
if a
1
elif b
opt
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)
;; an array and a Vec element
let xs: [3 i32?] = [1, None, x]
println(xs[0] ?? 0, xs[1] ?? 0, xs[2] ?? 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)
;; 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))
;; a kept chain: a T arm beside a T? arm makes the chain a T??
println(two(chain(true, false, none)), two(chain(false, true, none)), two(chain(false, false, none)))
let kk =
if x > 9
none
elif x > 1
1
println(two(kk))
;; generics
println(first(9), first(Some(8)), wrap(3) ?? 0)
;; a narrowed name still takes a payload value
let o: i32? = Some(1)
if o?
o = 10
println(o)