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

Replace Geometry interface with sum-type #87

Merged
merged 32 commits into from
Jan 6, 2020
Merged

Replace Geometry interface with sum-type #87

merged 32 commits into from
Jan 6, 2020

Conversation

peterstace
Copy link
Owner

@peterstace peterstace commented Jan 5, 2020

This change replaces the Geometry interface with a new Geometry type that can hold exactly one geometry value (i.e. it's a sum-type/variant/tagged-union). It also replaces the AnyGeometry type.

The new type is implemented as a pointer to the underlying geometry, along with a tag to indicate the type of geometry that it is.

Using the existing Geometry interface has a bunch of problems:

  • It's a very large interface (the bigger the interface, the weaker the abstraction).
  • Go interfaces are really designed so that new implementations that adhere to the behaviour expected by the interface can be subbed in. But that's not what we're using it for here... We typically switch on the implementation type and panic if it's not an expected type.
  • With using an interface, we have nowhere to implement logic specific to all geometries, or logic that applies to multiple geometries (since you can't add methods to an interface -- the methods can only go on the types that implement the interface).

A tagged union seems like a better match to the actual use-case.

@peterstace
Copy link
Owner Author

Note: I've self-reviewed this and am happy with the current CL.

Copy link
Collaborator

@den3tsou den3tsou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. Only concern is that unsafe package is not guaranteed for back-compatibility. But it is a small portion of code. Should be easy to change.

@peterstace
Copy link
Owner Author

Thanks for reviewing @Den3. Agreed, using the unsafe package is not ideal, but you're right -- it's only in a small number of places (a handful of 1 line methods restricted to a single struct). So if we for some reason need to change that part of the implementation it shouldn't be too tricky. Only using unsafe in a handful of places helps to manually verify that its usage is correct as well.

The alternative was to replace the ptr and tag fields with 9 pointers to each of the different geometry types (and only have one of them non-nil at a time). But then that might have made the Geometry type a little bit too heavy to pass around by value (72 bytes vs 16 bytes) -- and I'd rather have it passed around by value so that there is only a single redirection (rather than 2) when using the Geometry type.

@peterstace peterstace merged commit 08df3aa into master Jan 6, 2020
@peterstace peterstace deleted the sum_type branch January 6, 2020 18:52
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

Successfully merging this pull request may close these issues.

2 participants