I started looking into Go after playing around with structural typing in other languages like Scala and OCaml, and I'm trying map some of the idiomatic techniques between the languages. Consider the following types
type CoordinatePoint struct {
x int
y int
// Other methods and fields that aren't relevant
}
type CartesianPoint struct {
x int
y int
// Other methods and fields that aren't relevant
}
Let's say we'd like to write a method which operates on both of these types to compute their polar coordinate representations, func ConvertXYToPolar(point XYPoint) PolarPoint
. If the CartesianPoint
and CoordinatePoint
types defined getter and setter methods for the x
and y
fields we could define XYPoint
as a common interface with those methods, allowing us to operate on both types, but as it stands, interfaces cannot declare fields, only methods.
Based on this, I have a few questions:
- What is the idiomatic way of handling this in Go?
- Can it be done without modifying the existing types?
- Can we retain type safety, i.e. avoid defining
ConvertXYToPolar
without using the empty interface type as the parameter and manually converting? - If interfaces and implicit interface satisfaction are the primary tools for polymorphism in Go, is the forbiddence of fields in interface definitions limiting?
- Are getter/setter methods commonly defined on structs to circumvent this limitation?
- Is there a compelling reason behind the design decision not to support fields in interface definitions?
I find the simplicity of embedded types, implicit interface satisfaction, and interface-based polymorphism to be a very simple and appealing combination of techniques to promote code reusability and maintainability, but forbidding fields in interface definitions makes Go's structural typing capabilities somewhat limited from my perspective. Am I missing a simple solution?
func(Point) PolarPoint
and accept a CartesianPoint, for example. – Pattipattie