mirror of
https://github.com/vlang/v.git
synced 2025-09-18 11:56:57 -04:00
parent
e5153e7be7
commit
c6809844bd
@ -165,11 +165,8 @@ fn (mut c Checker) struct_decl(mut node ast.StructDecl) {
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
field_is_generic := field.typ.has_flag(.generic)
|
field_is_generic := field.typ.has_flag(.generic)
|
||||||
if !c.ensure_generic_type_specify_type_names(field.typ, field.type_pos, c.table.final_sym(field.typ).kind in [
|
if c.table.type_kind(field.typ) != .alias
|
||||||
.array,
|
&& !c.ensure_generic_type_specify_type_names(field.typ, field.type_pos, c.table.final_sym(field.typ).kind in [.array, .array_fixed, .map], field_is_generic) {
|
||||||
.array_fixed,
|
|
||||||
.map,
|
|
||||||
], field_is_generic) {
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
if field_is_generic {
|
if field_is_generic {
|
||||||
|
69
vlib/v/tests/generics/generic_alias_map_type_test.v
Normal file
69
vlib/v/tests/generics/generic_alias_map_type_test.v
Normal file
@ -0,0 +1,69 @@
|
|||||||
|
module main
|
||||||
|
|
||||||
|
import arrays
|
||||||
|
|
||||||
|
pub struct Tree {
|
||||||
|
pub:
|
||||||
|
// Type of structural node, `value` should be empty
|
||||||
|
type string
|
||||||
|
// Content of data node, `type` should be empty
|
||||||
|
value string
|
||||||
|
pub mut:
|
||||||
|
// Child nodes
|
||||||
|
kids []&Tree
|
||||||
|
}
|
||||||
|
|
||||||
|
@[params]
|
||||||
|
struct TreeCloneParams {
|
||||||
|
kids []&Tree
|
||||||
|
}
|
||||||
|
|
||||||
|
// Makes new derived node with different kids id defined.
|
||||||
|
pub fn (tree Tree) clone(p TreeCloneParams) &Tree {
|
||||||
|
return &Tree{
|
||||||
|
type: tree.type
|
||||||
|
value: tree.value
|
||||||
|
kids: p.kids
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Collection of hask tools for processing tree.
|
||||||
|
pub type TreeBelt = map[string]fn (input &Tree, belt TreeBelt) []&Tree
|
||||||
|
|
||||||
|
// Hask tool for processing node.
|
||||||
|
pub type TreeHack = fn (input &Tree, belt TreeBelt) []&Tree
|
||||||
|
|
||||||
|
pub type TreeContext = map[string]string
|
||||||
|
|
||||||
|
@[params]
|
||||||
|
struct TreeHackParams {
|
||||||
|
belt TreeBelt
|
||||||
|
context ?TreeContext
|
||||||
|
}
|
||||||
|
|
||||||
|
// Transform tree through context with transformers
|
||||||
|
pub fn (tree Tree) hack(p TreeHackParams) []&Tree {
|
||||||
|
return arrays.concat(arrays.flatten(tree.kids.map(it.hack_self(p))))
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn (tree Tree) hack_self(p TreeHackParams) []&Tree {
|
||||||
|
mut handle := fn [tree] (input &Tree, belt TreeBelt) []&Tree {
|
||||||
|
return [
|
||||||
|
input.clone(kids: input.hack(belt: belt)),
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
if action := p.belt[tree.type] {
|
||||||
|
handle = action
|
||||||
|
}
|
||||||
|
|
||||||
|
return handle(tree, p.belt)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn test_main() {
|
||||||
|
t := Tree{}
|
||||||
|
r := t.hack_self(belt: TreeBelt(map[string]fn (&Tree, TreeBelt) []&Tree{}))
|
||||||
|
assert r[0].type == ''
|
||||||
|
assert r[0].value == ''
|
||||||
|
assert r[0].kids.len == 0
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user