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

References to Interfaces maybe shouldn't be pointers? #58

Open
flyinprogrammer opened this issue May 12, 2024 · 3 comments
Open

References to Interfaces maybe shouldn't be pointers? #58

flyinprogrammer opened this issue May 12, 2024 · 3 comments

Comments

@flyinprogrammer
Copy link

What I expected:

Given a class that extends an abstract class, like Person:

When you reference a Person in another class as nullable, like with Bug:

Then, we should generate a reference to the Person interface because interfaces themselves are pointers and nil by default.

What I have observed:

Then, we generate a reference to a pointer of Person:

type Bug struct {
// The owner of this bug.
Owner *Person `pkl:"owner"`

What is the problem?

When we use a Bug and access the Owner, we have to dereference the value to access the data and create named constructors to use our structs:

https://github.com/flyinprogrammer/pkl-go-bananas/blob/7210f20ca30593f9d0ebad3665bcec1ebed6a128/main.go#L9-L28

func NewPerson() earth.Person {
	return &earth.PersonImpl{
		FirstName: "Alan",
		LastName:  "Scherger",
	}
}
func main() {
	alanPerson := NewPerson()

	atlasMoth := earth.Bug{
		Name:  "Addison",
		Owner: &alanPerson,
	}

	fmt.Println("Type of alanPerson: ", reflect.TypeOf(alanPerson))
	fmt.Println("Type of atlasMoth: ", reflect.TypeOf(atlasMoth))
	me := *atlasMoth.Owner
	fmt.Println("Type of me: ", reflect.TypeOf(me))
	fmt.Println("atlasMoth Owner GetFirstName: ", (*atlasMoth.Owner).GetFirstName())
	fmt.Println("me PersonImpl FirstName: ", me.(*earth.PersonImpl).FirstName)

}

This is very awkward.


Maybe we can add logic to stop adding a pointer to interfaces. That could be super hard. I don't know 😢 but I can come back to this later if no one else does; I don't have the time at this moment.

@bioball
Copy link
Contributor

bioball commented May 29, 2024

Makes sense. We can probably add an option to prefer non-pointers.

@thomaspurchas
Copy link

@flyinprogrammer does #83 address your concerns here?

@flyinprogrammer
Copy link
Author

@thomaspurchas I believe so! I'm more than happy to validate, too, post-merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants