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?
{x, {OptionValue[a], OptionValue[b]}}works. – J. M.'s missing motivation Sep 08 '17 at 16:34Attributes[OptionValue]isProtected... – user42582 Sep 08 '17 at 16:39OptionValue[{a,b}]works... – user42582 Sep 08 '17 at 16:41f[x_, OptionsPattern[]] := {x, OptionValue[f, #] & /@ {a, b}}allowsf[4]to produce{4, {1, 2} }. – jjc385 Sep 08 '17 at 16:41OptionValue[a]work inside the definition offwithout the need ofOptionValue[f,a]-no need 'explaining' that we are interested inf``'s optiona`? – user42582 Sep 08 '17 at 17:06OptionValueis 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 evenf[x_, OptionsPattern[]] := {x, OptionValue[#] & /@ {a, b}}works. – jjc385 Sep 08 '17 at 17:06