1 Answer
1

There are two problems I see with this code. The first one, and the one which gives you immediate problem you reported, is that, as it looks, short (magical) form of OptionValue does not work with SubValues - based definitions. So, you should replace

OptionValue["type"]

with

OptionValue[{opts}, "type"]

Or, if you wish to associate the option with pump directly and use it within the SubValue, you need to use

OptionValue[pump, {opts}, "type"]

instead.

The second, "conceptual" problem, is that you are really trying to use options for function dispatch, but in doing this your way, you give up the powerful rule-based function dispatch mechanism that is naturally available in mma. This is what I would do instead:

I delegated the dispatch to a new (extra) symbol dispatch, introduced specifically for this purpose. This looks cleaner and more extensible to me than using Which statement.

EDIT

Addressing the issue raised in comments: note that, as another manifestation of OptionValue - OptionsPattern magic being lost for SubValues- based functions, the above solution, as written, will not provide the intended default behavior, when the "type" option is not explicitly given. To make that work, one can do:

Everything works fine, with the exception that "type" default value ("plot") isn't being considered. If I set no option, it ends up calling dispatch[pump, "type"] (which isn't defined)
–
P. FonsecaApr 30 '12 at 13:44

@P.Fonseca It looks like this is another manifestation of OptionValue - OptionsPattern having lost some of their magic properties for SubValues. Just change OptionValue[{opts}, "type"] to OptionValue[{opts,"type" -> "plot"}, "type"] and it should work.
–
Leonid ShifrinApr 30 '12 at 15:12

Your last suggestion works. Thank you. When you can, please update your answer so that both "solutions" are shown: the one that should work if the magic was fully integrated (and that will, one day, eventually work...), and the one with your work around from your comment. Than you again...
–
P. FonsecaApr 30 '12 at 19:17

Mathematica is a registered trademark of Wolfram Research, Inc. While the mark is used herein with the limited permission of Wolfram Research, Stack Exchange and this site disclaim all affiliation therewith.