2
$\begingroup$

I am trying to produce a function with a few sets of arguments, one of which can accept options. However it doesn't seem to be responding to the changes that I accept.

ClearAll[testFn]
Options[testFn]={
    "myOption"->"defaultValue"
    };
testFn["A"][x_][y_,OptionsPattern[testFn]][z_]:={x,y,z,OptionValue[testFn,"myOption"]}

testFn["A"][1][2,"myOption"->"something"][3]
expectedOutput={1,2,3,"something"}

However this produces

{1, 2, 3, "defaultValue"}
{1, 2, 3, "something"}

I cannot seem to get the option to be anything except the default value in the first line of the input. Is there something I'm not doing correctly, if so, how can I fix it?

$\endgroup$
0

1 Answer 1

1
$\begingroup$

Need to pass the options to OptionValue as well as the head testFn:

ClearAll[testFn]
Options[testFn] = {"myOption" -> "defaultValue"};
testFn["A"][x_][y_, opts : OptionsPattern[]][z_] := {x, y, z, 
  OptionValue[testFn, {opts}, "myOption"]}

testFn["A"][1][2][3]
testFn["A"][1][2, "myOption" -> "something"][3]
(*
  {1, 2, 3, "defaultValue"}
  {1, 2, 3, "something"}
*)
$\endgroup$
2
  • $\begingroup$ Awesome, exactly what I was looking for. Thanks! $\endgroup$
    – user47636
    Commented Dec 15, 2018 at 0:15
  • $\begingroup$ @george You're welcome. $\endgroup$
    – Michael E2
    Commented Dec 15, 2018 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.