Skip to main content
17 votes
Accepted

Define function that behaves almost identically to Mathematica function

If you want to constrain it to only options from ListPlot, you could use OptionsPattern in combination with ...
chuy's user avatar
  • 11.2k
14 votes

Why is this syntax for patterns with Optional values not supported?

The Documentation states (emphasis is mine): Pattern (:): s:obj represents the pattern object ...
Alexey Popkov's user avatar
14 votes
Accepted

define a function with optional arguments

You must add a restricting pattern to the second parameter. Define your function like this: ...
Mr.Wizard's user avatar
  • 272k
13 votes

Functions with both optional arguments and options

This is a common problem when we want to allow both options (name -> value) and optional arguments (arguments that take a default value when omitted) in a ...
Szabolcs's user avatar
  • 236k
12 votes
Accepted

Confused by the opts : OptionsPattern[ ] pattern

Re 1: As documented in Operator Input Forms, colon represents two different operations: symb:expr Pattern[symb, expr] patt:expr Optional[patt, expr] If you ...
Itai Seggev's user avatar
  • 14.2k
12 votes
Accepted

Optional argument that can be completely omitted?

See @AlbertRetey's answer for all but trivial cases. Don't use Optional, nor If. Use two definitions. ...
Szabolcs's user avatar
  • 236k
12 votes

Are Mathematica commands always supposed to check that Method -> name is valid?

The other answer is completely correct (see my comment), but let me add some context. For algorthmic functions, it is generally the case that we validate the method options and issue messages/fail (...
Itai Seggev's user avatar
  • 14.2k
11 votes
Accepted

Functions with both optional arguments and options

As already said in a comment, I would not treat what feels more like an option as an usual argument to the function. Instead you can use OptionsPattern and friends. ...
Sascha's user avatar
  • 8,489
11 votes

Optional argument that can be completely omitted?

for your simple example Szabolcs suggestions is certainly the best you can do. If for some reason in a less simple situation you want the behavior you described with just one definition this is what ...
Albert Retey's user avatar
  • 23.6k
11 votes
Accepted

Are Mathematica commands always supposed to check that Method -> name is valid?

I would expect a fully implemented/documented function to check the validity of the specified Method; however, since ...
Bob Hanlon's user avatar
  • 160k
10 votes

Except @ OptionsPattern[] affects OptionValue

Recall that OptionValue is a "magic" symbol with nonstandard behavior. The expression OptionValue["bar"] "by magic" gets its ...
Mr.Wizard's user avatar
  • 272k
9 votes

Optional argument that can be completely omitted?

I'd love to have a short syntax form for that. I'd use it more often: similarWords[string_, n:(_|PatternSequence[]) ]:= Nearest[WordList[],string,n]
Kuba's user avatar
  • 137k
9 votes

Define function that behaves almost identically to Mathematica function

The usual way to define a Wolfram Language function that takes n arguments and an arbitrary number of options is like this: ...
rafalc's user avatar
  • 1,359
9 votes
Accepted

Why Optional in Set is not held by the HoldFirst attribute?

From the "Details" of the docs for HoldPattern The left-hand sides of rules are usually evaluated, as are parts of the left-hand sides of assignments. You can ...
Michael E2's user avatar
  • 238k
8 votes

Optional argument that can be completely omitted?

This seems to work: similarWords[string_, n: Repeated[_,{0,1}]] := Nearest[WordList[], string, n]
celtschk's user avatar
  • 19.2k
6 votes

Specifying optional arguments with variables

Another possibility is to use HoldPattern: var = 2; HoldPattern[f[a_: var]] := {a}; Then: ...
QuantumDot's user avatar
  • 19.7k
6 votes
Accepted

How to make a customized plot function that can plot functions with optional parameter?

I would use SubValues: define your functions with more than one []. This both makes it easier to write the plotting function, ...
Marius Ladegård Meyer's user avatar
6 votes
Accepted

Issue using optional argument before options

Make the pattern explicitly not match: ...
b3m2a1's user avatar
  • 46.9k
6 votes
Accepted

OptionValue not inserting correct option

Compile holds its arguments, and doesn't recognize the unevaluated Rule[_, _OptionValue] as an option. Use ...
Jason B.'s user avatar
  • 68.9k
5 votes
Accepted

How to write a `HeldOptional` variant of `Optional` that does not evaluate its second argument?

Putting together idea from question to use function with HoldRest attribute, together with idea from QuantumDot's answer to use ...
jkuczm's user avatar
  • 15.1k
5 votes
Accepted

What is the difference between the second argument of ToString and its option FormatType?

There is no difference between using a secong argument which is a member of $PrintForms and using the option FormatType. If the ...
Itai Seggev's user avatar
  • 14.2k
5 votes
Accepted

Why is { } interpreted as an option in my function?

O.k. from the comments and the OptionsPattern documentation: Any nesting of empty lists will match OptionsPattern I ...
MeMyselfI's user avatar
  • 1,116
5 votes

Keep irrelevant variables outside function arguments

Clear["Global`*"] f1[x_, y_ : y] := x^2 + y f2[x_, y_ : y] := (1 + x) f1[x, y] f3[x_, y_ : y] := Derivative[1, 0][f1][x, y] + f2[x + 1, y] In each case, ...
Bob Hanlon's user avatar
  • 160k
5 votes
Accepted

Function with complicated argument constraint with default value

In this case, the first : which is the shorthand of Pattern cannot be omitted (yeah n_ is ...
xzczd's user avatar
  • 67k
4 votes

Evaluation of FilterRules inside a Module

From the documentation, Show allows any option that can be applied to graphics to be given. But if you try to look at the options specific to ...
Jason B.'s user avatar
  • 68.9k
4 votes

How to Combine Pattern Constraints and Default Values for Function Arguments?

As a complement, you can also use: Clear[foo] foo[n:_Integer?Positive:1]:=n That filters the foo function argument according ...
Picaud Vincent's user avatar
4 votes
Accepted

Pattern matching with List and Optional

This bug is fixed in our next release. Note, however, that, that the use of Optional generates a warning now, as the optional value could never possible be used in such pattern: ...
Itai Seggev's user avatar
  • 14.2k
4 votes

How to define an option or an argument whose values are confined

...
Ali Hashmi's user avatar
  • 9,000
4 votes

OptionsPattern Confusion

Assuming that you do want to support options, I would do something like the following: ...
Carl Woll's user avatar
  • 131k
4 votes

How to prevent an Optional parameter from taking an OptionsPattern parameter without multiple function definitions?

Instead of using just a default, also include a pattern. For example: ...
Carl Woll's user avatar
  • 131k

Only top scored, non community-wiki answers of a minimum length are eligible