ClearAll@foo;
foo[tag_] := (
tag /: tag[i_] := tag[[i]]
);
a = {7, 8, 9};
foo[a];
a[2]
I wanted the above to output 8
, but encountered an error:
TagSetDelayed::sym: "Argument {7,8,9} at position 1 is expected to be a symbol. "
I think I understand that. It's currently a List, not a Symbol. So, I tried adding
SetAttributes[foo, HoldFirst];
after which I encountered another error:
TagSetDelayed::tagnf: "Tag a not found in {7,8,9}[i$_]."
Okay, so tag
evaluated, I believe because it was in the form _[___]
. So next, I added Unevaluated
:
foo[tag_] := (
tag /: Unevaluated[tag][i_] := tag[[i]]
);
But this resulted in
TagSetDelayed::tagpos: "Tag a in Unevaluated[a][i$_] is too deep for an assigned rule to be found."
This, I don't understand...