Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions vlib/v/tests/generics/generics_str_intp_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module main

import strings

pub struct MyStruct[T] {
pub mut:
result T
}

pub fn (it MyStruct[T]) indent_str[T]() string {
mut res := strings.new_builder(32)
res.write_string('${it.result}')
return res.str()
}

fn test_generics_str_intp() {
x := MyStruct[int]{
result: 100
}

y := MyStruct[string]{
result: 'hello'
}

assert x.indent_str() == '100'
assert y.indent_str() == 'hello'
}
5 changes: 5 additions & 0 deletions vlib/v/transformer/transformer.v
Original file line number Diff line number Diff line change
Expand Up @@ -1268,6 +1268,11 @@ pub fn (mut t Transformer) simplify_nested_interpolation_in_sb(mut onode ast.Stm
return false
}
original := nexpr.args[0].expr as ast.StringInterLiteral
if original.exprs.len != original.expr_types.len {
// This should be a generic type, e.g., `${it}` where `it` is type of T
// first time, `T` maybe `int`, but second time, `T` maybe `string`
return false
}
// only very simple string interpolations, without any formatting, like the following examples
// can be optimised to a list of simpler string builder calls, instead of using str_intp:
// >> sb.write_string('abc ${num}')
Expand Down
Loading