0
$\begingroup$

Trying to map OptionValue to a list of option names doesn't work.

options = {opt1, opt2, opt3};
Options[f] = # -> True & /@ options;
f[x_, o:OptionsPattern[]] := OptionValue /@ options;
f[x_] := f[x, Sequence@{}];

Executing

f[1, opt1 -> False]

gives the result

{OptionValue[opt1], OptionValue[opt2], OptionValue[opt3]}

instead of the expected

{False, True, True}

Why does this happen with OptionValue and can it be fixed?

$\endgroup$
16
  • $\begingroup$ Use f[x_, o : OptionsPattern[]] := Evaluate[OptionValue /@ options]; instead $\endgroup$ Feb 16 '14 at 23:53
  • $\begingroup$ @belisarius The problem with this is that the r.h.s. will evaluate immediately. $\endgroup$ Feb 16 '14 at 23:54
  • $\begingroup$ Here is the fix: f[x_, o : OptionsPattern[]] := OptionValue[#] & /@ options;. For an explanation, read this discussion. $\endgroup$ Feb 16 '14 at 23:56
  • $\begingroup$ In fact, this is an exact duplicate of that one, so I am voting to close... $\endgroup$ Feb 16 '14 at 23:57
  • $\begingroup$ @rm-rf Well, that was fast ... $\endgroup$ Feb 16 '14 at 23:58