4
$\begingroup$

I have a custom function like following:

SyntaxInformation[
   TwoNumberSameQ] = {"ArgumentsPattern" -> {_, _, OptionsPattern[]}};
Options[TwoNumberSameQ] = {ShowDifference -> False};
TwoNumberSameQ[x_, OptionsPattern[]] := {x, 
  OptionValue[ShowDifference]}
TwoNumberSameQ[a_, b_, OptionsPattern[]] := 
 If[OptionValue[ShowDifference], 
  If[a === b, True, {False, Abs[a - b]}], If[a === b, True, False]]

Usage:

TwoNumberSameQ[3,4]

False

TwoNumberSameQ[3,4,ShowDifference->True]

{False, 1}

 TwoNumberSameQ[3,3]

True

I have some dissatisfaction to this function.But the documentation have a little relative explanation for this area.

Question one

How to make the option have a black color like as a in-built function,but blue like:

Question two

How to make TwoNumberSameQ's definition more concise.I just test one option but result so many repetitive code.I cannot imagine how mess when I set three or more option.

Addtional quesiton

If I can make the option have some optional values to choice.I'll be happy more. :)

$\endgroup$
6
$\begingroup$
  1. Global` symbols are blue unless they have a value but if you use any other context that is on $ContextPath the symbol color will be black just because it was created.

    So use System` by mentioning option name when it is parsed for the first time:

    Options[TwoNumberSameQ] = {System`ShowDifference -> True}
    

    or define your function in Begin/EndPackage where the option name is exported. Keep in mind conflicts so choose your names wisely.

    BeginPackage["Numbers`"];
    
      TwoNumberSameQ; ShowDifference;
    
    Begin["`Private`"];
    
      (* your definitions *)
    End[];
    
    EndPackage[];
    

I believe rest of your questions was already answered in:

  1. How to specify and use nested options for a function?

    How to deal with the condition that a function own many options?

    Writing functions with "Method" options

  2. Prompt a set of possible options

$\endgroup$
  • $\begingroup$ You mean I should creat a specific context for those options? $\endgroup$ – yode Jul 11 '16 at 18:10
  • $\begingroup$ Maybe Szabolcs have prompt me the color render scheme in this answer,but I cann't apply it in such case.It's pitty.I don't very familiar these.Do you think the following question have a value to ask still? $\endgroup$ – yode Jul 11 '16 at 18:17
  • $\begingroup$ It's seem not anyone can get my English.I mean since those question already have solution so that I should close this post? :) $\endgroup$ – yode Jul 11 '16 at 19:40
  • $\begingroup$ @yode I know your pain! :) p.s. I think you can live that unless someone complains. $\endgroup$ – Kuba Jul 11 '16 at 20:17
  • $\begingroup$ Thanks for your consideration sincerely. $\endgroup$ – yode Jul 11 '16 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.