Skip to main content
added 3 characters in body
Source Link
Kuba
  • 138.9k
  • 13
  • 297
  • 803

###requirements / notes###additional requirements

###requirements / notes

###additional requirements

added 331 characters in body
Source Link
Kuba
  • 138.9k
  • 13
  • 297
  • 803
  • I don't think Options[f] = Join[customOptions, Options[Button], ...] is a good solution, there may be duplicates in custom for them and an explicit list of Options[f] grows.

  • But I still want to be able to provide e.g Appearance option to f even though it is not included in Options[f], with OptionsPattern I will get Unknown option Apparance for f... message.

  • without OptionsPattern we can't use built in OptionValue, but I want to be able to refer to functions by name

  1. I want to avoid Options[f] = Join[customOptions, Options[Button], ...].

I don't think is a good solution, there may be duplicates in customOptions for them and an explicit list of Options[f] grows.

  1. I want to be able to provide any option to the function without error messsage e.g.: Unknown option Apparance for f...

  2. We can get 2. by skipping OptionsPattern[] in definition but without it we can't use built in OptionValue. I want to be able to refer to functions by their names.

  3. Rules filtering mechanism should not produce duplicates. I know Button[..., ImageSize->300, ImageSize->200] will behave stable but I find it ugly.

###my approach

So I need to start my definitions with With[{ opt = mergeRules[ {optionsPattern}, Options[f]]}, which does not seem to be a big problem, but why I have to do this?

Is there simpler approach, with built functions maybe? Or should I include Options[Button] etc. to Options[f] and count on the fact that when given duplicates, first one wins?

###Edits

Mr.Wizard's answer fulfills points:

1 automatically, 2/3 by using OptionsPattern[{f,Button, ...}]. So still 4 needs custom filtering function but it is a good answer anyway.

  • I don't think Options[f] = Join[customOptions, Options[Button], ...] is a good solution, there may be duplicates in custom for them and an explicit list of Options[f] grows.

  • But I still want to be able to provide e.g Appearance option to f even though it is not included in Options[f], with OptionsPattern I will get Unknown option Apparance for f... message.

  • without OptionsPattern we can't use built in OptionValue, but I want to be able to refer to functions by name

###my approach

So I need to start my definitions with With[{ opt = mergeRules[{optionsPattern}, Options[f]]}, which does not seem to be a big problem, but why I have to do this?

Is there simpler approach, with built functions maybe? Or should I include Options[Button] etc. to Options[f] and count on the fact that when given duplicates, first one wins?

  1. I want to avoid Options[f] = Join[customOptions, Options[Button], ...].

I don't think is a good solution, there may be duplicates in customOptions for them and an explicit list of Options[f] grows.

  1. I want to be able to provide any option to the function without error messsage e.g.: Unknown option Apparance for f...

  2. We can get 2. by skipping OptionsPattern[] in definition but without it we can't use built in OptionValue. I want to be able to refer to functions by their names.

  3. Rules filtering mechanism should not produce duplicates. I know Button[..., ImageSize->300, ImageSize->200] will behave stable but I find it ugly.

###my approach

So I need to start my definitions with With[{ opt = mergeRules[ {optionsPattern}, Options[f]]}, which does not seem to be a big problem, but why I have to do this?

Is there simpler approach, with built functions maybe? Or should I include Options[Button] etc. to Options[f] and count on the fact that when given duplicates, first one wins?

###Edits

Mr.Wizard's answer fulfills points:

1 automatically, 2/3 by using OptionsPattern[{f,Button, ...}]. So still 4 needs custom filtering function but it is a good answer anyway.

Tweeted twitter.com/#!/StackMma/status/595575055842705408
added 1 character in body
Source Link
Kuba
  • 138.9k
  • 13
  • 297
  • 803

###the case I want to be able to create a function with some default options but also without need to add full explicit list of options available for it.

And then inside I want to be able to filter from given and default options, those which are Button options or Tooltip options for example.

So something like:

Options[f] = {(*list of default options*)} f[args__, OptionsPattern[]]:=Column[{ (*Options that are suitable for Button*), (*Options that are suitable for Tooltip*), OptionValue[(*specific name*)] }] 

And I wasn't able to get this with built in Options management functions: OptionsPattern[], OptionValue, FilterRules etc.

###requirements / notes

  • I don't think Options[f] = Join[customOptions, Options[Button], ...] is a good solution, there may be duplicates in custom for them and an explicit list of Options[f] grows.

  • But I still want to be able to provide e.g Appearance option to f even though it is not included in Options[f], with OptionsPattern I will get Unknown option Apparance for f... message.

  • without OptionsPattern we can't use built in OptionValue, but I want to be able to refer to functions by name

###my approach

(* auxiliary functions *) mergeRules = GatherBy[Join[##], First][[All, 1]] &; optionValue = #2 /. # &; (* function definition *) ClearAll[f]; Options[f] = {"Test" -> 1, ImageSize -> 100, TooltipDelay -> 20}; f[x_, optionsPattern : (_Rule | _RuleDelayed) ...] := With[{ opt = mergeRules[{optionsPattern}, Options[f]]} , Column@{ FilterRules[opt, Options@Button], FilterRules[opt, Options@Tooltip], optionValue[opt, "Test"] } ] 

So I need to start my definitions with With[{ opt = mergeRules[{optionsPattern}, Options[f]]}, which does not seem to be a big problem, but why I have to do this?

###tests

f[1, Appearance -> "Palette"] 
{Appearance->Palette, ImageSize->100}  
{TooltipDelay->20} 1 f[1, ImageSize -> 200] 
{ImageSize->200} 
{TooltipDelay->20} 1 f[1] 
{ImageSize->100} 
{TooltipDelay->20} 1 

###question

Is there simpler approach, with built functions maybe? Or should I include Options[Button] etc. to Options[f] and count on the fact that when given duplicates, first one wins?

###the case I want to be able to create a function with some default options but also without need to add full explicit list of options available for it.

And then inside I want to be able to filter from given and default options, those which are Button options or Tooltip options for example.

So something like:

Options[f] = {(*list of default options*)} f[args__, OptionsPattern[]]:=Column[{ (*Options that are suitable for Button*), (*Options that are suitable for Tooltip*), OptionValue[(*specific name*)] }] 

And I wasn't able to get this with built in Options management functions: OptionsPattern[], OptionValue, FilterRules etc.

###requirements / notes

  • I don't think Options[f] = Join[customOptions, Options[Button], ...] is a good solution, there may be duplicates in custom for them and an explicit list of Options[f] grows.

  • But I still want to be able to provide e.g Appearance option to f even though it is not included in Options[f], with OptionsPattern I will get Unknown option Apparance for f... message.

  • without OptionsPattern we can't use built in OptionValue, but I want to be able to refer to functions by name

###my approach

(* auxiliary functions *) mergeRules = GatherBy[Join[##], First][[All, 1]] &; optionValue = #2 /. # &; (* function definition *) ClearAll[f]; Options[f] = {"Test" -> 1, ImageSize -> 100, TooltipDelay -> 20}; f[x_, optionsPattern : (_Rule | _RuleDelayed) ...] := With[{ opt = mergeRules[{optionsPattern}, Options[f]]} , Column@{ FilterRules[opt, Options@Button], FilterRules[opt, Options@Tooltip], optionValue[opt, "Test"] } ] 

So I need to start my definitions with With[{ opt = mergeRules[{optionsPattern}, Options[f]]}, which does not seem to be a big problem, but why I have to do this?

###tests

f[1, Appearance -> "Palette"] 
{Appearance->Palette, ImageSize->100} 
{TooltipDelay->20} 1 f[1, ImageSize -> 200] 
{ImageSize->200} 
{TooltipDelay->20} 1 

###question

Is there simpler approach, with built functions maybe? Or should I include Options[Button] etc. to Options[f] and count on the fact that when given duplicates, first one wins?

###the case I want to be able to create a function with some default options but also without need to add full explicit list of options available for it.

And then inside I want to be able to filter from given and default options, those which are Button options or Tooltip options for example.

So something like:

Options[f] = {(*list of default options*)} f[args__, OptionsPattern[]]:=Column[{ (*Options that are suitable for Button*), (*Options that are suitable for Tooltip*), OptionValue[(*specific name*)] }] 

And I wasn't able to get this with built in Options management functions: OptionsPattern[], OptionValue, FilterRules etc.

###requirements / notes

  • I don't think Options[f] = Join[customOptions, Options[Button], ...] is a good solution, there may be duplicates in custom for them and an explicit list of Options[f] grows.

  • But I still want to be able to provide e.g Appearance option to f even though it is not included in Options[f], with OptionsPattern I will get Unknown option Apparance for f... message.

  • without OptionsPattern we can't use built in OptionValue, but I want to be able to refer to functions by name

###my approach

(* auxiliary functions *) mergeRules = GatherBy[Join[##], First][[All, 1]] &; optionValue = #2 /. # &; (* function definition *) ClearAll[f]; Options[f] = {"Test" -> 1, ImageSize -> 100, TooltipDelay -> 20}; f[x_, optionsPattern : (_Rule | _RuleDelayed) ...] := With[{ opt = mergeRules[{optionsPattern}, Options[f]]} , Column@{ FilterRules[opt, Options@Button], FilterRules[opt, Options@Tooltip], optionValue[opt, "Test"] } ] 

So I need to start my definitions with With[{ opt = mergeRules[{optionsPattern}, Options[f]]}, which does not seem to be a big problem, but why I have to do this?

###tests

f[1, Appearance -> "Palette"] 
{Appearance->Palette, ImageSize->100}  
{TooltipDelay->20} 1 f[1, ImageSize -> 200] 
{ImageSize->200} 
{TooltipDelay->20} 1 f[1] 
{ImageSize->100} 
{TooltipDelay->20} 1 

###question

Is there simpler approach, with built functions maybe? Or should I include Options[Button] etc. to Options[f] and count on the fact that when given duplicates, first one wins?

Source Link
Kuba
  • 138.9k
  • 13
  • 297
  • 803
Loading