checker: support ?T, &T generic return types (#6772)
parent
b47c23b73e
commit
931882d720
|
@ -1565,7 +1565,13 @@ pub fn (mut c Checker) call_fn(mut call_expr ast.CallExpr) table.Type {
|
|||
// Handle `foo<T>() T` => `foo<int>() int` => return int
|
||||
return_sym := c.table.get_type_symbol(f.return_type)
|
||||
if return_sym.source_name == 'T' {
|
||||
return call_expr.generic_type
|
||||
mut typ := call_expr.generic_type
|
||||
typ = typ.set_nr_muls(f.return_type.nr_muls())
|
||||
if f.return_type.has_flag(.optional) {
|
||||
typ = typ.set_flag(.optional)
|
||||
}
|
||||
call_expr.return_type = typ
|
||||
return typ
|
||||
} else if return_sym.kind == .array {
|
||||
elem_info := return_sym.info as table.Array
|
||||
elem_sym := c.table.get_type_symbol(elem_info.elem_type)
|
||||
|
|
|
@ -102,6 +102,30 @@ fn test_return_array() {
|
|||
assert a4 == [true, false, true]
|
||||
}
|
||||
|
||||
fn opt<T>(v T) ?T {
|
||||
if sizeof(T) > 1 {return v}
|
||||
return none
|
||||
}
|
||||
|
||||
fn test_optional() {
|
||||
s := opt('hi') or { '' }
|
||||
assert s == 'hi'
|
||||
i := opt(5) or {0}
|
||||
assert i == 5
|
||||
b := opt(s[0]) or {99}
|
||||
assert b == 99
|
||||
}
|
||||
|
||||
fn ptr<T>(v T) &T {
|
||||
a := [v]
|
||||
return a.data
|
||||
}
|
||||
|
||||
fn test_ptr() {
|
||||
assert *ptr(4) == 4
|
||||
assert *ptr('aa') == 'aa'
|
||||
}
|
||||
|
||||
/*
|
||||
fn map_f<T,U>(l []T, f fn(T)U) []U {
|
||||
mut r := []U{}
|
||||
|
|
Loading…
Reference in New Issue