mirror of
https://github.com/vlang/v.git
synced 2025-09-15 10:27:19 -04:00
This commit is contained in:
parent
bb99f8b57c
commit
c4aaa2ef4d
@ -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
|
if fmt == `_` { // set default representation for type if none has been given
|
||||||
fmt = c.get_default_fmt(ftyp, typ)
|
fmt = c.get_default_fmt(ftyp, typ)
|
||||||
if fmt == `_` {
|
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)}`',
|
c.error('no known default format for type `${c.table.get_type_name(ftyp)}`',
|
||||||
node.fmt_poss[i])
|
node.fmt_poss[i])
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
pub fn (mut ct ComptimeInfo) unwrap_generic_expr(expr ast.Expr, default_typ ast.Type) ast.Type {
|
||||||
match expr {
|
match expr {
|
||||||
|
ast.StringLiteral, ast.StringInterLiteral {
|
||||||
|
return ast.string_type
|
||||||
|
}
|
||||||
ast.ParExpr {
|
ast.ParExpr {
|
||||||
return ct.unwrap_generic_expr(expr.expr, default_typ)
|
return ct.unwrap_generic_expr(expr.expr, default_typ)
|
||||||
}
|
}
|
||||||
|
21
vlib/v/tests/generics/generic_lambda_inference_test.v
Normal file
21
vlib/v/tests/generics/generic_lambda_inference_test.v
Normal file
@ -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
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user