When g is in the local scope defined by Block, the following fails:
Block[{g, x},
g[x] = StringExpression[x : LetterCharacter];
SetAttributes[f, Listable];
f = Function[{str}, StringMatchQ[str, g[x]], Listable];
]
eg. f[{"v", "e", "7", "2", "i"}] produces the message StringExpression::invld with text "Element g[x] is not a valid string or pattern element in g[x]" and returns
{StringMatchQ["v", g[x]], StringMatchQ["e", g[x]], StringMatchQ["7", g[x]], StringMatchQ["2", g[x]], StringMatchQ["i", g[x]]}
but instead when g is in the global scope ie when g is removed from the list of variables that are local to the Block, f works as expected.
On the previous example, evaluating f[{"v", "e", "7", "2", "i"}] returns {True, True, False, False, True}.
(Why is this happening and) would it be possible to make f work when g is local to the Block?
FunctionisHoldAlland onceBlockis done there is no reference togwhich is insidef. You need to inject it withWithorFunction[...] /. DownValues[g]– Kuba May 03 '18 at 08:31Withdoesn't work withg[x]but/. DownValues[g]does the trick; a quick follow-up: when I definefas inSetAttributes[f, Listable]; f[str_] := StringMatchQ[str, g[x]], the problem persists for the same reasons? – user42582 May 03 '18 at 08:57SetDelayedholds right hand side too. – Kuba May 03 '18 at 09:01