Hi everyone,
I have developed a paclet (to be shared soon), and as a final touch I’m trying to make some custom objects behave in a more “WL native” way.
I have noticed that many objects in WL have associated information that can be accessed by using the Information function.
For example, with neural nets:
I have developed a paclet (to be shared soon), and as a final touch I’m trying to make some custom objects behave in a more “WL native” way.
I have noticed that many objects in WL have associated information that can be accessed by using the Information function.
For example, with neural nets:
In[]:=
net=NetChain[{4,Ramp,2,Ramp,3}]
Out[]=
NetChain
In[]:=
info=Information[net]
Out[]=
In[]:=
Information[net,"ArraysCount"]
Out[]=
6
Underneath this, there seems to be an an InformationData object associated to NetChain:
In[]:=
FullForm[info]
Out[]//FullForm=
InformationData[Association[Rule["LayersCount",5],Rule["ArraysCount",6],Rule["SharedArraysCount",0],Rule["InputPortNames",List["Input"]],Rule["OutputPortNames",List["Output"]],Rule["ArraysTotalElementCount",Indeterminate],Rule["ArraysTotalSize",Indeterminate],Rule["ObjectType","NetInformation"]]]
My question is: How can I implement my own InformationData object so that I can access information about custom objects in this way?
For the purpose of illustration, suppose I have defined MyObject:
For the purpose of illustration, suppose I have defined MyObject:
ClearAll[MyObject,MyObjectQ];MyObject[a_Integer] := MyObject[<|"List"->Range[a]|>]MyObjectQ[asc_?AssociationQ] := KeyExistsQ[asc, "List"](* Make it appear in "iconised" form *)MyObject/:MakeBoxes[obj:MyObject[asc_?MyObjectQ],form:(StandardForm|TraditionalForm)] := Module[{info}, info={{BoxForm`SummaryItem[{"Length: ",Length@asc["List"]}]}}; BoxForm`ArrangeSummaryBox[MyObject,obj,None,info,{},form,"Interpretable"->Automatic]];
Let’s create an instance of it:
In[]:=
obj=MyObject[3]
Out[]=
MyObject
Suppose I have some function that compute some property of MyObject instances, for example:
In[]:=
getSomeProperty[obj_MyObject]:=MinMax[obj[[1]]["List"]]
In[]:=
getSomeProperty[obj]
Out[]=
{1,3}
What should I do to access such information using or instead of calling getSomeProperty[obj]?
Information[obj]
Information[obj,"SomeProperty"]