This question already has an answer here:

I want to create a function that creates and assigns properties to a symbol. In theory this would behave similarly to the NonzeroValues property of SparseArrays.

Here's what I currently have, and where I'd like to go.

func[x_] := Module[{data},
  (*data=Import data from source based on x*)
  HoldPattern[func[x, "Length"]] = Length@data;
  HoldPattern[func[x, "Dimensions"]] = Dimensions@data;



However, I'd like this to work more like this:




It would be ideal not to have to calculate this every time a property is retrieved. For efficiency sake, importing the data every time a property is retrieved isn't feasible.

How so I define these subvalues at the first function call and store them for fast retrieval later?


merged by Mr.Wizard Apr 14 '14 at 20:46

This question was merged with How to implement FittedModel like objects because it is an exact duplicate of that question.