# Why doesn't OptionValue cooperate with Map? [duplicate]

Consider the following definition:

Options[f] = {a -> 1, b -> 2}
f[x_,OptionsPattern[]] := {x,OptionValue/@{a,b}}


I thought that doing something like f[4] would produce {4,{1,2}} but it doesn't. Instead, evaluating f[4] returns {4, {OptionValue[a], OptionValue[b]}}.

Is there a way to make OptionValue/@{a,b} produce {1, 2}? Is there a better way to achieve the same effect?

• There is likely some special evaluation at play here, since {x, {OptionValue[a], OptionValue[b]}} works. – J. M.'s discontentment Sep 8 '17 at 16:34
• Note that f[x_, OptionsPattern[]] := {x, OptionValue[f, #] & /@ {a, b}} allows f[4] to produce {4, {1, 2} }. – jjc385 Sep 8 '17 at 16:41
• I understand your point. A relevant question is why does OptionValue[a] work inside the definition of f without the need of OptionValue[f,a]-no need 'explaining' that we are interested in f's option a? – user42582 Sep 8 '17 at 17:06
• See this answer, especially the second paragraph, which suggests that OptionValue is able to figure out which function it appears in only when it's given explicit arguments in the function definition (before any evaluation occurs). Actually, it turns out even f[x_, OptionsPattern[]] := {x, OptionValue[#] & /@ {a, b}} works. – jjc385 Sep 8 '17 at 17:06
• I have marked this question as already has an answer -- please review the linked post, and if you feel that it does not address your question edit yours to specifically describe how your question or needs differ. – Mr.Wizard Sep 8 '17 at 17:27

Options[f] = {a -> 1, b -> 2};

f[4]
`