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$
2
$\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$
  • $\begingroup$ Awesome, exactly what I was looking for. Thanks! $\endgroup$ – george Dec 15 '18 at 0:15
  • $\begingroup$ @george You're welcome. $\endgroup$ – Michael E2 Dec 15 '18 at 0:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.