We had discussed aliases a few times in the last years. I know that some people uses templates as alias to save some keystrokes, and I tried that myself for the first time now:
I would like to write e.x1 instead of e.p1.x for example.
type
V2 = tuple[x, y: float]
type
Element = ref object of RootRef
p1, p2: V2
template x1(e: Element): var float = e.p1.x
#template `x1=`(e: var Element; v: float) = e.p1.x = v
var
e = Element()
e2 = Element(p1: (0.0, 3.14))
proc t(w: float) =
echo w
t(e.p1.x)
x1(e) = 2.7
e.x1 = 2.7 # this line needs the second template to compile
t(e.x1)
t(x1(e))
My hope was that using var for the template return type would allow me an assignment, so that I would not have to define an additional x1= setter template. But that seems not to work for the method call syntax.
/tmp/hhh/t.nim(21, 6) Error: undeclared field: 'x1' for type t.Element
This seems to work:
template `.=`(x: typed, y: untyped, z: typed): untyped =
y(x) = z
However, it looks dangerous.