Skip to main content
added 20 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

I too like Rojo's answer. And Mr. Wizard's answer is intriguing as always. A third possibility is adding the condition that the Heads of second and third arguments are not Rule after the LHS. So, the following also works

 Options[f] = {"g" -> Identity};
 f[x_, y_: 2, z_: 3,  OptionsPattern[]] /; (Head[y] =!= Rule && Head[z] =!= Head[z]Rule) := 
 OptionValue["g"][x + y + z]

The condition could be alternatively stated as OptionQ[y]=(OptionQ[y] =!=True== True && OptionQ[z] =!=OptionQ[z]= True).

I too like Rojo's answer. And Mr. Wizard's answer is intriguing as always. A third possibility is adding the condition that the Heads of second and third arguments are not Rule after the LHS. So, the following also works

 Options[f] = {"g" -> Identity};
 f[x_, y_: 2, z_: 3,  OptionsPattern[]] /; (Head[y] =!= Rule =!= Head[z]) := 
 OptionValue["g"][x + y + z]

The condition could be alternatively stated as OptionQ[y]=!=True=!=OptionQ[z].

I too like Rojo's answer. And Mr. Wizard's answer is intriguing as always. A third possibility is adding the condition that the Heads of second and third arguments are not Rule after the LHS. So, the following also works

 Options[f] = {"g" -> Identity};
 f[x_, y_: 2, z_: 3,  OptionsPattern[]] /; (Head[y] =!= Rule && Head[z] =!= Rule):= 
 OptionValue["g"][x + y + z]

The condition could be alternatively stated as (OptionQ[y] =!= True && OptionQ[z] =!= True).

added 84 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

I too like Rojo's answer. And Mr. Wizard's answer is intriguing as always. A third possibility is adding the condition that the Heads of second and third arguments are not Rule after the LHS. So, the following also works

 Options[f] = {"g" -> Identity};
 f[x_, y_: 2, z_: 3,  OptionsPattern[]] /; (Head[y] =!= Rule =!= Head[z]) := 
 OptionValue["g"][x + y + z]

The condition could be alternatively stated as OptionQ[y]=!=True=!=OptionQ[z].

I too like Rojo's answer. And Mr. Wizard's answer is intriguing as always. A third possibility is adding the condition that the Heads of second and third arguments are not Rule after the LHS. So, the following also works

 Options[f] = {"g" -> Identity};
 f[x_, y_: 2, z_: 3,  OptionsPattern[]] /; (Head[y] =!= Rule =!= Head[z]) := 
 OptionValue["g"][x + y + z]

I too like Rojo's answer. And Mr. Wizard's answer is intriguing as always. A third possibility is adding the condition that the Heads of second and third arguments are not Rule after the LHS. So, the following also works

 Options[f] = {"g" -> Identity};
 f[x_, y_: 2, z_: 3,  OptionsPattern[]] /; (Head[y] =!= Rule =!= Head[z]) := 
 OptionValue["g"][x + y + z]

The condition could be alternatively stated as OptionQ[y]=!=True=!=OptionQ[z].

added 28 characters in body
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929

SpecifyingI too like Rojo's answer. And Mr. Wizard's answer is intriguing as always. A third possibility is adding the condition that the Head of the last argument insteads of the first two optionalsecond and third arguments are not Rule after the LHS. So, the following also works

 Options[f] = {"g" -> Identity}; 
 f[x_, y_: 2, z_: 3, opts : OptionsPattern[]] /; (Head[y] =!= Rule =!= Head[z]) := 
 OptionValue["g"][x + y + z]

Specifying the Head of the last argument instead of the first two optional arguments, the following also works

 Options[f] = {"g" -> Identity}; 
 f[x_, y_: 2, z_: 3, opts : OptionsPattern[]] := OptionValue["g"][x + y + z]

I too like Rojo's answer. And Mr. Wizard's answer is intriguing as always. A third possibility is adding the condition that the Heads of second and third arguments are not Rule after the LHS. So, the following also works

 Options[f] = {"g" -> Identity};
 f[x_, y_: 2, z_: 3,  OptionsPattern[]] /; (Head[y] =!= Rule =!= Head[z]) := 
 OptionValue["g"][x + y + z]
Post Undeleted by kglr
Post Deleted by kglr
Source Link
kglr
  • 400.5k
  • 18
  • 488
  • 929
Loading