Note: This question could be a duplicate but I couldn't find any so far.
It is known that Optional
(:) can be use to represent expression if omitted.
f[x_, y_:0]:= {x, y};
f[a]
(*{a,0}*)
Now what should I do so that I can use a sequence of values in the Optional
, something like this:
f[x_, y_(if omitted gives sequence of 1,2,3)]:= {x, y};
so that If I evaluate f[a]
I get {a,1,2,3}.
The method f[x_, y_: Sequence[1, 2, 3]] := {x, y}
will not work because of the evaluation that happens at the time of defining f
.
Of course this can be solved by giving HoldAll
Attributes to the function f
:
SetAttributes[f, HoldAll]
f[x_, y_: Sequence[1, 2, 3]] := {x, y}
f[a]
(*{a, 1, 2, 3}*)
But this needs to set the attributes which some times is not desired.
Question:
Is there a way to add sequence as Optional without the need to use Attributes?
Thank you
f[1,3,4]
? $\endgroup$f[x_,y__:options]
instead off[x_,y_:options]
. Basically I am designing a graphical function which takes default styles if nothing inputted. After all, I think this approach is not the best way to do that because if I have more than one default style and I need to change only one I need then to input all of the default including the new one. May be I need to useOptionsPattern
andOptionValue
$\endgroup$Directive
. $\endgroup$Directive
otherwise the inputted style will be the only one will pass to RHS. For example,f[x_, y_: Directive[style1, style2]]
if I want to changestyle2
I need to do it like thisf[ firstinput, Directive[style1, newstyle]]
$\endgroup$