I'm trying to use Associations as object, and specifically want to use them as they are used in other languages using the dot notation. I'm using the code below to get started.
Unprotect[Dot];
Dot[a_Association, p_Symbol] :=
Return[Evaluate[ a[p]]];
Protect[Dot];
This works fine for many things.
a=<|firstName->"Joe",lastName->"Smith",age->20|>;
a.firstName
(* Joe *)
But, it does not work for other things
a.age^2
a.firstName <> " " <> a.lastName
I know there are things I can do to resolve this issue, but the best way is eluding me.
Thanks in advance.
Precedence[Dot]
is fairly low compared to that ofPower
andStringJoin
. For example,(a.age)^2
and(a.firstName) <> " " <> (a.lastName)
will work. I don't know exactly how the precedence should be changed, though. $\endgroup$.
has very low precedence in WL: tutorial/OperatorInputForms way too low for an access operator. You can put()
around but that is not a point. I'd say it is impossible in MMA because you don't have access to FE parser and patching Kernel parser will be a huge task.a.age^2
is automaticallyRowBox[{"a", ".", RowBox[{"age", "^", "2"}]}]]
so you are not even starting from a plain input form string but already structured input. $\endgroup$