# Define an access by key ( structure[key] ) for a custom structure struct?

I have some "complicated" data that I want to encapsulate into a "struct":

For instance (imagine that data have several fields):

data=<|"A"->6,"B"->2|>
var=myType[data]


doSomething[myType[d_]]:=d

(* usage: *)
doSomething[var]


Now I want to allow this:

var["B"] <- must return 2


But I do not know how to do that, any idea?

What I have done so far:

myType /: Key[k_][myType[d_]:=d[k]


This works for syntax like:

Key["B"][var]


but not for

var["B"]


ClearAll[myType]

• This definitely works, though it's good to notice that this sets a SubValue for myType, not an UpValue. – Sjoerd Smit Feb 1 '19 at 13:35