File tree Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Expand file tree Collapse file tree 2 files changed +29
-2
lines changed Original file line number Diff line number Diff line change @@ -4387,15 +4387,15 @@ fn (g &Gen) expr_is_multi_return_call(expr ast.Expr) bool {
4387
4387
}
4388
4388
4389
4389
fn (mut g Gen) gen_result_error (target_type ast.Type, expr ast.Expr) {
4390
- styp := g.typ (target_type)
4390
+ styp := g.typ (g. unwrap_generic ( target_type) )
4391
4391
g.write ('(${styp} ){ .is_error=true, .err=' )
4392
4392
g.expr (expr)
4393
4393
g.write (', .data={EMPTY_STRUCT_INITIALIZATION} }' )
4394
4394
}
4395
4395
4396
4396
// NB: remove this when optional has no errors anymore
4397
4397
fn (mut g Gen) gen_optional_error (target_type ast.Type, expr ast.Expr) {
4398
- styp := g.typ (target_type)
4398
+ styp := g.typ (g. unwrap_generic ( target_type) )
4399
4399
g.write ('(${styp} ){ .state=2, .err=' )
4400
4400
g.expr (expr)
4401
4401
g.write (', .data={EMPTY_STRUCT_INITIALIZATION} }' )
Original file line number Diff line number Diff line change
1
+ module main
2
+
3
+ fn get_value [T]() ? T {
4
+ return none
5
+ }
6
+
7
+ fn get_value_result [T]() ! T {
8
+ return error ('no result' )
9
+ }
10
+
11
+ fn test_generic_function_that_returns_an_option () {
12
+ value := get_value[& int ]() or { & int (0 ) }
13
+ assert value == unsafe { nil }
14
+ sval := get_value[string ]() or { 'abc' }
15
+ assert sval == 'abc'
16
+ uval := get_value[u64 ]() or { 123 }
17
+ assert uval == 123
18
+ }
19
+
20
+ fn test_generic_function_that_returns_an_error () {
21
+ sval := get_value_result[string ]() or { 'xyz' }
22
+ assert sval == 'xyz'
23
+ ival := get_value_result[int ]() or { 456 }
24
+ assert ival == 456
25
+ pval := get_value_result[& int ]() or { & int (789 ) }
26
+ assert u64 (pval) == u64 (& int (789 ))
27
+ }
You can’t perform that action at this time.
0 commit comments