-
Notifications
You must be signed in to change notification settings - Fork 33
Description
What I expected:
Given a class that extends an abstract class, like Person:
open class Person extends Being { type Person interface {
When you reference a Person in another class as nullable, like with Bug:
pkl-go/codegen/snippet-tests/input/Classes.pkl
Lines 81 to 83 in 4541166
class Bug { /// The owner of this bug. owner: Person? Owner *Person `pkl:"owner"`
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:
pkl-go/codegen/snippet-tests/output/bugholder/Bug.pkl.go
Lines 10 to 12 in 4541166
| 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:
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.