diff --git a/vlib/v/tests/sumtype_with_alias_fntype_fn_call_test.v b/vlib/v/tests/sumtype_with_alias_fntype_fn_call_test.v new file mode 100644 index 0000000000..a95d8a01f4 --- /dev/null +++ b/vlib/v/tests/sumtype_with_alias_fntype_fn_call_test.v @@ -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' +} diff --git a/vlib/v/tests/sumtype_with_alias_fntype_test.v b/vlib/v/tests/sumtype_with_alias_fntype_test.v index 8136be6e4b..416b5112b8 100644 --- a/vlib/v/tests/sumtype_with_alias_fntype_test.v +++ b/vlib/v/tests/sumtype_with_alias_fntype_test.v @@ -11,25 +11,18 @@ 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' - } + Myfnfact { return 'yes fn' } + None { return 'None fn' } } } fn test_sumtype_with_alias_fntype() { + // create Myfn + myfnfact := fn () Myfn { + return abc + } r1 := main.myfnfact()(1) println(r1) assert r1 == 1 @@ -40,5 +33,5 @@ fn test_sumtype_with_alias_fntype() { r3 := run(myfnfact) println(r3) - assert r3 == 'yes fn: fn (int) int' + assert r3 == 'yes fn' }