4

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. :)

yode
  • 26,686
  • 4
  • 62
  • 167

1 Answers1

6
  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

Kuba
  • 136,707
  • 13
  • 279
  • 740
  • You mean I should creat a specific context for those options? – yode Jul 11 '16 at 18:10
  • 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? – yode Jul 11 '16 at 18:17
  • It's seem not anyone can get my English.I mean since those question already have solution so that I should close this post? :) – yode Jul 11 '16 at 19:40
  • @yode I know your pain! :) p.s. I think you can live that unless someone complains. – Kuba Jul 11 '16 at 20:17
  • Thanks for your consideration sincerely. – yode Jul 11 '16 at 20:30