vlang

/

v Public
0 commits 39 issues 0 pull requests 0 contributors Discussions Projects CI

V error with receiver method on interface #36

Describe the bug

V is giving an error about the parameter to the function not implementing the interface fields. But it's testing for the wrong interface. The fn (mut node Node) append_child(child &Node) is what is giving the error. The compiler is expecting an Element interface there for some reason. Using this instead where fn append_child is invoked does work.

element.children << &Node(&Text{
    name: 'text'
    text: 'Hello, World!'
})

Reproduction Steps

interface Node {
    id string
    name string
mut:
    children []&Node
}

fn (mut node Node) append_child(child &Node) {
    node.children << child
}

interface Element {
    Node
    attributes map[string]string
}

[heap]
struct NodeBase {
    id   string
    name string
mut:
    children []&Node
}

[heap]
struct Text {
    NodeBase
    text string
}

[heap]
struct HTMLBodyElement {
    NodeBase
    attributes map[string]string
}

fn main() {
    mut element := &Element(&HTMLBodyElement{
        name: 'body'
    })
    element.append_child(&Node(&Text{
        name: 'text'
        text: 'Hello, World!'
    }))
    println(element)
}

Expected Behavior

output:

&Element(HTMLBodyElement{
    NodeBase: NodeBase{
        id: ''
        name: 'body'
        children: [&Node(Text{
    NodeBase: NodeBase{
        id: ''
        name: 'text'
        children: []
    }
    text: 'Hello, World!'
})]
    }
    attributes: {}
})

Current Behavior

output:

C:/Users/imado/Documents/test.v:41:23: error: cannot implement interface `Element` with a different interface `&Node`
   39 |         name: 'body'
   40 |     })
   41 |     element.append_child(&Node(&Text{
      |                          ~~~~~~~~~~~~
   42 |         name: 'text'
   43 |         text: 'Hello, World!'
C:/Users/imado/Documents/test.v:41:23: error: `&Node` doesn't implement field `attributes` of interface `Element`
   39 |         name: 'body'
   40 |     })
   41 |     element.append_child(&Node(&Text{
      |                          ~~~~~~~~~~~~
   42 |         name: 'text'
   43 |         text: 'Hello, World!'

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.2 37e5616

Environment details (OS name and version, etc.)

V full version: V 0.4.2 37e5616
OS: windows, Microsoft Windows 11 Pro v22621 64-bit
Processor: 16 cpus, 64bit, little endian, 

getwd: C:\Users\imado\Documents\cyberian_tiger
vexe: C:\Users\imado\v\v.exe
vexe mtime: 2023-10-09 22:29:31

vroot: OK, value: C:\Users\imado\v
VMODULES: OK, value: C:\Users\imado\.vmodules
VTMP: OK, value: C:\Users\imado\AppData\Local\Temp\v_0

Git version: git version 2.33.1.windows.1
Git vroot status: weekly.2023.40.1-50-g37e5616b (9 commit(s) behind V master)
.git/config present: true

CC version: Error: 'cc' is not recognized as an internal or external command,
operable program or batch file.

thirdparty/tcc status: thirdparty-windows-amd64 e90c2620

[!NOTE] You can vote for this issue using the 👍 reaction. More votes increase the issue's priority for developers.

Take into account that only the 👍 reaction counts as a vote. Only reactions to the issue itself will be counted as votes, not comments.