mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
parent
f0a23c8d3c
commit
f6cc88fa69
44
vlib/v/tests/sumtype_with_alias_fntype_fn_call_test.v
Normal file
44
vlib/v/tests/sumtype_with_alias_fntype_fn_call_test.v
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
struct None {}
|
||||||
|
|
||||||
|
type Myfn = fn (int) int
|
||||||
|
|
||||||
|
type Myfnfact = fn () Myfn
|
||||||
|
|
||||||
|
type Maybefnfact = Myfnfact | None
|
||||||
|
|
||||||
|
// Myfn
|
||||||
|
fn abc(i int) int {
|
||||||
|
return i
|
||||||
|
}
|
||||||
|
|
||||||
|
// create Myfn
|
||||||
|
fn myfnfact() Myfn {
|
||||||
|
return abc
|
||||||
|
}
|
||||||
|
|
||||||
|
// run fn if exists
|
||||||
|
fn run(mmff Maybefnfact) string {
|
||||||
|
match mmff {
|
||||||
|
Myfnfact {
|
||||||
|
r := mmff()
|
||||||
|
return 'yes fn: ${r}'
|
||||||
|
}
|
||||||
|
None {
|
||||||
|
return 'None fn'
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_sumtype_with_alias_fntype_fn_call() {
|
||||||
|
r1 := main.myfnfact()(1)
|
||||||
|
println(r1)
|
||||||
|
assert r1 == 1
|
||||||
|
|
||||||
|
r2 := run(None{})
|
||||||
|
println(r2)
|
||||||
|
assert r2 == 'None fn'
|
||||||
|
|
||||||
|
r3 := run(myfnfact)
|
||||||
|
println(r3)
|
||||||
|
assert r3 == 'yes fn: fn (int) int'
|
||||||
|
}
|
@ -11,25 +11,18 @@ fn abc(i int) int {
|
|||||||
return i
|
return i
|
||||||
}
|
}
|
||||||
|
|
||||||
// create Myfn
|
|
||||||
fn myfnfact() Myfn {
|
|
||||||
return abc
|
|
||||||
}
|
|
||||||
|
|
||||||
// run fn if exists
|
|
||||||
fn run(mmff Maybefnfact) string {
|
fn run(mmff Maybefnfact) string {
|
||||||
match mmff {
|
match mmff {
|
||||||
Myfnfact {
|
Myfnfact { return 'yes fn' }
|
||||||
r := mmff()
|
None { return 'None fn' }
|
||||||
return 'yes fn: ${r}'
|
|
||||||
}
|
|
||||||
None {
|
|
||||||
return 'None fn'
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn test_sumtype_with_alias_fntype() {
|
fn test_sumtype_with_alias_fntype() {
|
||||||
|
// create Myfn
|
||||||
|
myfnfact := fn () Myfn {
|
||||||
|
return abc
|
||||||
|
}
|
||||||
r1 := main.myfnfact()(1)
|
r1 := main.myfnfact()(1)
|
||||||
println(r1)
|
println(r1)
|
||||||
assert r1 == 1
|
assert r1 == 1
|
||||||
@ -40,5 +33,5 @@ fn test_sumtype_with_alias_fntype() {
|
|||||||
|
|
||||||
r3 := run(myfnfact)
|
r3 := run(myfnfact)
|
||||||
println(r3)
|
println(r3)
|
||||||
assert r3 == 'yes fn: fn (int) int'
|
assert r3 == 'yes fn'
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user