Another workaround
type
# The 3 notations refer to the same 3-D entity, and some coordinates are
shared
CoordinateSystem = enum
csCar, # Cartesian (x,y,z)
csCyl, # Cylindrical (r,φ,z)
csSph # Spherical (ρ,θ,φ)
Coordinates = object
cs: CoordinateSystem # cs is the coordinate discriminator
coor1, coor2, coor3: float # keep this part private
CoorException* = ref object of Exception
proc x(p: Coordinates): float =
if p.cs != csCar:
raise newException(Defect, "Only available for Cartesian.")
p.coor1
proc `x=`(p: var Coordinates, xval: float) =
if p.cs != csCar:
raise newException(Defect, "Only available for Cartesian.")
p.coor1 = xval
# do the same with y, and z, should be succint with templates
proc φ*(p: Coordinates): float =
if p.cs == csCar:
raise newException(Defect, "Cartesian doesn't support φ field.")
elif p.cs == csCyl:
result = p.coor2
elif p.cs == csSph:
result = p.coor3
# do the same with other field
var car1 = Coordinates(cs: csCar,
coor1: 1.0,
coor2: 2.0,
coor3: 3.0)
echo car1.x
car1.x = 1.5
echo car1.x
var car2 = Coordinates(cs: csCyl,
coor1: 1.0,
coor2: 2.0,
coor3: 3.0)
echo car2.φ
Run
Read more about it in [manual page
here](https://nim-lang.github.io/Nim/manual.html#procedures-properties)