Message Boards Message Boards

1
|
2364 Views
|
3 Replies
|
4 Total Likes
View groups...
Share
Share this post:

Function with OptionsPattern

Posted 3 years ago

I define a function:

func3[a_,OptionsPattern[$opt1->False]]:=Module[{junk}, Print[OptionValue[$opt1]]]

It works as expected when invoked as:

func3["abc",$opt1->True]

But, if I type: Options[func3] I get '{}' as the response rather than (as I expected) '{$opt1->False}'

If I type: Options[Plot] or any other system function

I get the options for the system function. The docs for Options don't mention any restrictions the function specified. But, I can't get it to work for any of my (user) functions.

POSTED BY: Terry Stewart
3 Replies
Posted 3 years ago

Glad to help!

Looking at the documentation to remind me, the OptionsPattern you used is for supplying an on-the-fly list of defaults, which I've seen used to implement "hidden options" (a.k.a. undocumented options), i.e. options that don't show up when you evaluate Options[f].

POSTED BY: Joel Klein
Posted 3 years ago

I've never used OptionsPattern that way. I always declare the options list for my function and then use OptionsPattern with no arguments (I also never see option symbols that have a $ in the name, that's usually used to mark a constant or a setting you can reset):

Options[func3] = {opt1 -> True}
func3[a_, OptionsPattern[]] := Module[{}, Print[OptionValue[opt1]]]

Then Options[func3] will return your options list and OptionValue inside the function will be able to look up the options for func3.

POSTED BY: Joel Klein

Thanks much, your suggestion worked as advertised. Being a newbie to Mathematica, I suspect I will do a lot of things that you (and others) haven't seen before.

POSTED BY: Terry Stewart
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract