Timeline for How to avoid collision between optional arguments and options?
Current License: CC BY-SA 4.0
16 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
S Oct 25, 2021 at 18:10 | history | suggested | lineage | CC BY-SA 4.0 |
ordering agnostic adverb
|
Oct 25, 2021 at 13:51 | review | Suggested edits | |||
S Oct 25, 2021 at 18:10 | |||||
Aug 3, 2017 at 12:45 | comment | added | Kuba |
Didn't know about OptionQ , thanks.
|
|
Apr 13, 2017 at 12:55 | history | edited | CommunityBot |
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
|
|
Sep 9, 2016 at 13:39 | comment | added | Szabolcs |
Something that bit me in the past about optional arguments and options was not noticing that OptionsPattern[] can match not only a sequence of options, but also any nested list of options. Luckily, OptionQ also returns true for such lists! Thus this is not a loophole in your method. But the discovery that OptionQ[{a->1, b->2}] is True is new to me. EDIT: Well, it's pretty clearly stated in the usage messages, but I didn't look there ...
|
|
Jun 23, 2014 at 5:56 | comment | added | saturasl |
@AndyRoss, as to your nr = x_/;!OptionQ[x] , according to The Standard Evaluation Procedure, in f[x_,y:nr:2,z:nr:3,OptionsPattern[]]:=... , the patterns x_,y:nr:2,z:nr:3,OptionsPattern[] will firstly be evaluated in order. If any of the y or z Condition fails then the entire definition of f will be considered unmatched and the follwing SetDelayed will not be applied. As a result, if we call f[1,"g"->(#^2&)] then the "g"->(#^2&) will firstly be checked with the y:nr:2 which yields unmatch.
|
|
Aug 7, 2013 at 13:25 | history | edited | Mr.Wizard | CC BY-SA 3.0 |
added 52 characters in body
|
Feb 10, 2012 at 10:21 | comment | added | Mr.Wizard | @Andy I was wrong. See kguler's answer below. | |
Feb 10, 2012 at 9:42 | history | edited | Mr.Wizard | CC BY-SA 3.0 |
added 463 characters in body
|
Feb 10, 2012 at 8:27 | comment | added | Mr.Wizard |
@Andy I mean that if the condition fails the entire definition of f does not match.
|
|
Feb 10, 2012 at 8:09 | comment | added | Andy Ross | @MrWizard I guess I'm just not getting it.. its 2:00 am here so that may be why :) What I'm seeing from your example is obvious, the 3rd arg will not admit anything smaller than 5. But !OptionQ will exclude only Rule, {}, and RuleDelayed, (I think). | |
Feb 10, 2012 at 8:01 | comment | added | Mr.Wizard |
@Andy Condition applies to the entire function pattern. Think about f[1, 2, x_ /; x > 5] := ... and f[1, 2, 3]
|
|
Feb 10, 2012 at 7:45 | comment | added | Andy Ross |
@MrWizard This is pretty cool, why does this work when nr = x_/;!OptionQ[x] does not?
|
|
Feb 10, 2012 at 7:42 | history | undeleted | Mr.Wizard | ||
Feb 10, 2012 at 7:41 | history | deleted | Mr.Wizard | ||
Feb 10, 2012 at 7:39 | history | answered | Mr.Wizard | CC BY-SA 3.0 |