Let's say I want to overload the built-in function Lookup
, to handle my data wrapped in myObject
. One would use TagSet
or TagsetDelayed
(f /: lhs = rhs
or f /: lhs := rhs
) like this:
ClearAll[myObject];
myObject /: Lookup[o_myObject, args___] := Lookup[First[o], args];
This won't work because the HoldAllComplete
attribute of Lookup
prevents checking the head of the argument (everything is done internal to Lookup
, which I don't want to meddle with).
obj = myObject[<|a -> 1, b -> 2|>]
Lookup[Evaluate[obj], b]
Lookup::invrl: The argument myObject[<|a->1,b->2|>] is not a valid Association or a list of rules. >>
Is there any way to overload such a function to handle arbitrary input wrappers?
DownValues
toLookup
, and I wouldn't do that).HoldAllComplete
is hard to bargain with. But if you define your own data type, you could define your own heads for lookup. So, what are the cases where this is a real problem then? $\endgroup$withLookup = Function[code, Unevaluated[code] /. HoldPattern[Lookup[o_myObject, args___]]:>Lookup[First@obj, args], HoldAll]
. But this is of course limited and can't be made fully dynamic without introducing a fully typed code (type system). $\endgroup$[]
is not nice), but I have to accept the limits. That is my main issue with Mathematica recently: it is no use introducing new, compact objects (a laFittedModel
for example) as one cannot really integrate it with System functions properly. $\endgroup$myObject
), not to clutter the screen. But then I wanted to have all theAssociation
-related facilties to work with it (likeNormal
,Join
,Lookup
,Dataset
, etc.). And then I realized atLookup
that I couldn't do it. And I didn't want to introduce my own functions to go withmyObject
, as Leonid has suggested. $\endgroup$