6
$\begingroup$

I have a simple object orientation example using this solution:

MyObject[assn_Association][args__String] := assn[args]
initMyObject[assn_Association] := With[{a = Merge[{assn, <|"SomeProperty"->2|>}, First]}, MyObject[assn]]

You can create the object:

In[1]:= o = initMyObject[<|"a" -> 1|>]
Out[1]= MyObject[<|"a" -> 1|>]

You can access the object:

In[2]:= o["a"]
Out[2]= 1

And it looks like you can assign:

In[3]:= o["SomeProperty"] = 22
Out[3]= 22

But it doesn't work:

In[4]:= FullForm @ o
Out[4]= MyObject[Association[Rule["a",1]]]

Is there a way to make this syntax (obj["property"] = value) work here?

Update

I thought I could so something like this:

MyObject /: Set[m_MyObject[arg_String], val_] := Set[m, alteredCopy[m,<|arg->val|>]]

but this an other variants doesn't work...

$\endgroup$