Message Boards Message Boards

Passing options to function as a list rather than individual arguments?

GROUPS:

I see code like this that works:

show3D[graphics_] := Show[graphics, {ImageSize -> localImageSize}]

It works similarly if the list of options contains more than one option; and it works if the list of options contains zero options.

Normally, I see options strung as individual arguments at the end of the list of arguments; not inside any list:

show3D[graphics_] := Show[graphics, ImageSize -> localImageSize]

That's what I've seen in many examples in the documentation, iirc. Does putting them inside a list make any difference? Thank you.

POSTED BY: Joe Donaldson
Answer
13 days ago

That depends a bit on the implementation of these functions. If the option passing is implemented with OptionsPattern[] then providing lists of options should work fine. I think, before OptionsPattern[] was introduced (version 6.0), the usual way was to implement functions with optional arguments by using BlankNullSequence like f[arg1_,opt____]:=.... (Actually I don't know if this has ever been the "official" way of doing that but I have seen something like that in undocumented functions several times when I was spelunking.) So it depended a bit on the developers wether they processed the options with Flatten[List[opt]] or not. So, rather old functions that were coded before OptionsPattern[] was introduced and haven't been touched since then might get problems...

POSTED BY: Henrik Schumacher
Answer
13 days ago

Thank you.

POSTED BY: Joe Donaldson
Answer
13 days ago

You're welcome.

POSTED BY: Henrik Schumacher
Answer
13 days ago

Group Abstract Group Abstract