Consider an association which contains a key which must not be prematurely evaluated.
assoc = <| key :> value |>
For example, a value of a[[b]]
would throw a Part
error until symbols a
and b
are substituted.
We might wish to obtain this value in its unevaluated form, e.g. to display. Simply calling
assoc[key]
will evaluate value
(and in that example, throw a Part
error).
One trick is to exploit that matched patterns are unevaluated:
assoc /. KeyValuePattern[ key :> v_ ] -> Hold[v]
but this seems needlessly hacky for what must be a common operation.
What's the right way to obtain Hold[value]
?