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

Cannot compile program that contains method that has pointer type receiver #58

Closed
shouji-kazuo opened this issue Apr 4, 2019 · 7 comments
Labels
Bug This tag is applied to issues which reports bugs.

Comments

@shouji-kazuo
Copy link

shouji-kazuo commented Apr 4, 2019

Steps to reproduce:

  1. Enter program list as below:
struct Foobar {
  member int
}

fn (f *Foobar) do() { // method of pointer type receiver
  println('I am foobar')
}

fn main() {
  f := Foobar{}
  f.do() // I think f is type of "Foobar", but I assume that f can treat as pointer type with implicit conversion ( like Golang )
} 
  1. Click Run button
  2. The output was shown as below.
    You just found a bug. V can't compile this program, but it should. Please create a GitHub issue.
@medvednikov
Copy link
Member

The correct syntax is

struct Foobar {
	member int
}

fn (f mut Foobar) do() {
	// method of pointer type receiver
	println('I am foobar')
}

fn main() {
	f := Foobar{}
	f.do()// I assume f is type of "Foobar", but can treat as pointer type with implicit conversion ( like Golang )
}

@medvednikov
Copy link
Member

(f mut Foobar) instead of (f *Foobar)

I'll update the compiler so that (f *Foobar) doesn't compile.

@medvednikov
Copy link
Member

I assume f is type of "Foobar", but can treat as pointer type with implicit conversion ( like Golang )

This assumption is correct :)

@shouji-kazuo
Copy link
Author

OK, I understood.
ありがとうございました m(_ _)m

@medvednikov
Copy link
Member

Fixed.

You should now get use (f mut Foobar) instead of (f *Foobar)

@shouji-kazuo
Copy link
Author

I have confirmed .
thanks a lot!

@ntrel
Copy link
Contributor

ntrel commented Apr 4, 2019

f is type of "Foobar", but can treat as pointer type with implicit conversion

I don't think letting pointers implicitly dereference is a good idea, and it seems to go against the simple philosophy of V:

mut i := 5
p := &i
i = p // confusing, should be an error
i = *p // OK

Edit: See #61

Larpon pushed a commit to Larpon/v that referenced this issue Feb 24, 2022
@medvednikov medvednikov added the Bug This tag is applied to issues which reports bugs. label Jul 19, 2022
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.
Projects
None yet
Development

No branches or pull requests

3 participants