Skip to content

Commit

Permalink
checker: fix generic lambda with late concrete type inference (fix vl…
Browse files Browse the repository at this point in the history
  • Loading branch information
felipensp authored Oct 13, 2024
1 parent bb99f8b commit c4aaa2e
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 1 deletion.
2 changes: 1 addition & 1 deletion vlib/v/checker/str.v
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ fn (mut c Checker) string_inter_lit(mut node ast.StringInterLiteral) ast.Type {
if fmt == `_` { // set default representation for type if none has been given
fmt = c.get_default_fmt(ftyp, typ)
if fmt == `_` {
if typ != ast.void_type {
if typ != ast.void_type && !(c.inside_lambda && typ.has_flag(.generic)) {
c.error('no known default format for type `${c.table.get_type_name(ftyp)}`',
node.fmt_poss[i])
}
Expand Down
3 changes: 3 additions & 0 deletions vlib/v/comptime/comptimeinfo.v
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,9 @@ fn (mut ct ComptimeInfo) comptime_get_kind_var(var ast.Ident) ?ast.ComptimeForKi

pub fn (mut ct ComptimeInfo) unwrap_generic_expr(expr ast.Expr, default_typ ast.Type) ast.Type {
match expr {
ast.StringLiteral, ast.StringInterLiteral {
return ast.string_type
}
ast.ParExpr {
return ct.unwrap_generic_expr(expr.expr, default_typ)
}
Expand Down
21 changes: 21 additions & 0 deletions vlib/v/tests/generics/generic_lambda_inference_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const result = ['0: a', '1: b', '2: c', '3: d']

fn mapi[T, U](arr []T, callback fn (int, T) U) []U {
mut mapped := []U{}
for i, el in arr {
mapped << callback(i, el)
}
return mapped
}

fn test_main() {
arr := [`a`, `b`, `c`, `d`]
arr_1 := mapi(arr, |i, e| '${i}: ${e}')
assert arr_1 == result
arr_2 := mapi[rune, string](arr, |i, e| '${i}: ${e}')
assert arr_2 == result
arr_3 := mapi(arr, fn (i int, e rune) string {
return '${i}: ${e}'
})
assert arr_3 == result
}

0 comments on commit c4aaa2e

Please sign in to comment.