Message Boards Message Boards

0
|
5276 Views
|
3 Replies
|
1 Total Likes
View groups...
Share
Share this post:

[?] Define an option or an argument whose values are confined?

Hello everyone, I want to define a function whose arguments or options can only be selected in a range. For example:

Options[TestFunction]={problemID->1};       
TestFunction[x_, opts:OptionsPattern[]]:=Module[{},...];

Here, the problemID must one value of {1, 2, 3, 4}, and the default value is 1.

My question is how to define such argument or options. For example, that is the argument like the second argument of build-in function Import[], whose second argument can only be selected in {Element, Data, List, CSV, .... and so on}.

Thank you very much!

POSTED BY: Kui Chen
3 Replies

You might try the definition, like e.g.:

testFunction[x_, problemID_: 1] := (whatEverFunctionBody[x, problemID]) /; MemberQ[ {1, 2, 3, 4}, problemID]
POSTED BY: Henrik Schachner

That is one way of doing it, but that's not an option ;)

POSTED BY: Sander Huisman

This is kind of tricky for options what generally happens is that the function throws a message when the options are not correct (out of bound). See for example how it is done in the built-in Plot function, you can see how the arguments are parsed et cetera:

Needs["GeneralUtilities`"]
Plot[Sin[x], {x, 0, 1}];
PrintDefinitions@System`ProtoPlotDump`iPlotLayer1
PrintDefinitions@System`ProtoPlotDump`iPlot
POSTED BY: Sander Huisman
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