Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V error with receiver method on interface #19550

Open
islonely opened this issue Oct 11, 2023 · 3 comments
Open

V error with receiver method on interface #19550

islonely opened this issue Oct 11, 2023 · 3 comments
Labels
Bug This tag is applied to issues which reports bugs. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Type System Bugs/feature requests, that are related to the V types system.

Comments

@islonely
Copy link
Contributor

islonely commented Oct 11, 2023

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.

@islonely islonely added the Bug This tag is applied to issues which reports bugs. label Oct 11, 2023
@Delta456
Copy link
Member

I am not sure if receiver methods should work for interfaces.

@JalonSolov
Copy link
Contributor

I agree. An interface in V is more of a contract than a concrete item.

However, if it shouldn't be allowed, then V should give an error message when you try to create one.

@islonely
Copy link
Contributor Author

How it works now is that an interface can't implement it's own method, but can have methods on the type itself. https://github.com/vlang/v/blob/master/doc/docs.md#interface-method-definitions

This is not allowed.

interface Dog {
    bark()
}

fn (dog Dog) bark() { ... }

This is allowed.

interface Dog {}

fn (dog Dog) bark() { ... }

@ArtemkaKun ArtemkaKun added Unit: Type System Bugs/feature requests, that are related to the V types system. Unit: Checker Bugs/feature requests, that are related to the type checker. labels Oct 12, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Unit: Checker Bugs/feature requests, that are related to the type checker. Unit: Type System Bugs/feature requests, that are related to the V types system.
Projects
None yet
Development

No branches or pull requests

4 participants