Options
[f] You can assign values to Options
to specify options.
Options[f] = {n -> 2}
Options[f]
f[x_, OptionsPattern[f]] := x ^ OptionValue[n]
f[x]
f[x, n -> 3]
Delayed option rules are evaluated just when the corresponding OptionValue
is called:
f[a :> Print["value"]] /. f[OptionsPattern[{}]] :> (OptionValue[a]; Print["between"]; OptionValue[a]);
In contrast to that, normal option rules are evaluated immediately:
f[a -> Print["value"]] /. f[OptionsPattern[{}]] :> (OptionValue[a]; Print["between"]; OptionValue[a]);
Options must be rules or delayed rules:
Options[f] = {a}
A single rule need not be given inside a list:
Options[f] = a -> b
Options[f]
Options can only be assigned to symbols:
Options[a + b] = {a -> b}