Note that there are some explanatory texts on larger screens.

plurals
  1. POSpecifying settings for Options
    text
    copied!<p>How do you explicitly state the valid settings for an option? Take this example for instance</p> <pre><code>Options[myFunc] = {opt1 -&gt; "SomeString"}; myFunc[OptionsPattern[]] := Print[OptionValue[opt1]]; </code></pre> <p><code>myFunc</code> prints the value of the option. If we evaluate <code>myFunc[opt1 -&gt; {1, 2}]</code> then it prints <code>{1, 2}</code>. This function will essentially print anything that you set to <code>opt1</code>. My question is, how can I make sure that my function only accept a given number of values for <code>opt1</code>. We can start with something simple like a <code>String</code> and an <code>Integer</code>.</p> <p>To have a better idea of the behavior that we would except when given the wrong values for <code>opt1</code> we can take a look at what happens when we give the wrong values for <code>PlotRange</code> in the function <code>Plot</code>.</p> <p><img src="https://i.stack.imgur.com/as16Y.png" alt="enter image description here"></p> <p>In the example given in the picture I purposely gave wrong values to the <code>PlotRange</code> options and gave me a message specifying the correct type of values for that particular options. It seems that it <code>PlotRange</code> ended up taking its default value and thus it return the <code>Graphics</code> object.</p> <p>In the simple example what we would like to obtain is something like:</p> <pre><code>myFunc::sometag : Value of option opt1 -&gt; `1` is not a string or interger. </code></pre> <p>Does anyone know how to achieve this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload