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

Equal generation requires pointer types to be generated correctly #81

Open
jamietanna opened this issue Mar 27, 2023 · 2 comments
Open

Comments

@jamietanna
Copy link
Contributor

jamietanna commented Mar 27, 2023

Following the example code on the README:

type MyStruct struct {
	Int64     int64
	StringPtr *string
}

func (this *MyStruct) Equal(that *MyStruct) bool {
	return deriveEqual(this, that)
}

// alternatively
func (this MyStruct) Equal(that MyStruct) bool {
	return deriveEqual(&this, &that)
}

Results in:

func deriveEqual(this, that *MyStruct) bool {
	return (this == nil && that == nil) ||
		this != nil && that != nil &&
			this.Int64 == that.Int64 &&
			((this.StringPtr == nil && that.StringPtr == nil) || (this.StringPtr != nil && that.StringPtr != nil && *(this.StringPtr) == *(that.StringPtr)))
}

However, when this uses a non-pointer receiver:

func (this MyStruct) Equal(that MyStruct) bool {
	return deriveEqual(this, that)
}

This generates:

func deriveEqual(this, that MyStruct) bool {
	return ((&this == nil && &that == nil) || (&this != nil && &that != nil && (*(&this)).Equal(*(&that))))
}

Which doesn't perform the same checks?

@awalterschulze
Copy link
Owner

Still technically correct, but you are right, we could do better here, by not doing the checks

@dimovnike
Copy link

Doesn't seem even technically correct because it causes infinite recursion and a stack overflow.

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

No branches or pull requests

3 participants