mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
parent
fc72044b42
commit
d1fdcfbab4
2 changed files with 26 additions and 1 deletions
|
@ -191,7 +191,9 @@ fn (mut g Gen) str_val(node ast.StringInterLiteral, i int, fmts []u8) {
|
||||||
} else if fmt == `s` || typ.has_flag(.variadic) {
|
} else if fmt == `s` || typ.has_flag(.variadic) {
|
||||||
mut exp_typ := typ
|
mut exp_typ := typ
|
||||||
if expr is ast.Ident {
|
if expr is ast.Ident {
|
||||||
if expr.obj is ast.Var {
|
if g.comptime.get_ct_type_var(expr) == .smartcast {
|
||||||
|
exp_typ = g.comptime.get_comptime_var_type(expr)
|
||||||
|
} else if expr.obj is ast.Var {
|
||||||
if expr.obj.smartcasts.len > 0 {
|
if expr.obj.smartcasts.len > 0 {
|
||||||
exp_typ = g.unwrap_generic(expr.obj.smartcasts.last())
|
exp_typ = g.unwrap_generic(expr.obj.smartcasts.last())
|
||||||
cast_sym := g.table.sym(exp_typ)
|
cast_sym := g.table.sym(exp_typ)
|
||||||
|
|
23
vlib/v/tests/comptime/comptime_variant_interp_test.v
Normal file
23
vlib/v/tests/comptime/comptime_variant_interp_test.v
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
module main
|
||||||
|
|
||||||
|
struct Foo {
|
||||||
|
a int
|
||||||
|
}
|
||||||
|
|
||||||
|
type SumType = Foo | int | string
|
||||||
|
|
||||||
|
fn (v SumType) cast[T](val T) string {
|
||||||
|
mut res := ''
|
||||||
|
$for variant_value in v.variants {
|
||||||
|
if v is variant_value {
|
||||||
|
println('v: ${v}')
|
||||||
|
res = 'v: ${v}'
|
||||||
|
println(res)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
assert SumType(1).cast[int](1) == 'v: 1'
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue