{"id":613,"date":"2022-08-30T15:04:16","date_gmt":"2022-08-30T15:04:16","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/how-do-i-add-options-to-a-built-in-function-which-only-apply-to-a-certain-class-of-argument-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:04:16","modified_gmt":"2022-08-30T15:04:16","slug":"how-do-i-add-options-to-a-built-in-function-which-only-apply-to-a-certain-class-of-argument-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/how-do-i-add-options-to-a-built-in-function-which-only-apply-to-a-certain-class-of-argument-collection-of-common-programming-errors\/","title":{"rendered":"How do I add options to a built in function, which only apply to a certain class of argument?-Collection of common programming errors"},"content":{"rendered":"<p>I&#8217;d like to add an option to a built-in function that only applies when it is given an argument of a certain form.<\/p>\n<p>For example, something like this approach to extending <code>DateString<\/code> to support an extra option when it is given a date argument that matches the form <code>zoned[...]<\/code> (forget that <code>zoned<\/code> does nothing here):<\/p>\n<pre><code>DateString[zoned[x_], spec_, opts : OptionsPattern[{TimeZone -&gt; 0,ShowZone-&gt;True}]] ^:= \nDateString[x, If[OptionValue[ShowZone],\n   Append[spec, If[OptionValue[TimeZone] != 0, \"UTC\"  ToString[OptionValue[TimeZone]], \"Z\"]], spec]]\n<\/code><\/pre>\n<p>Since <code>ShowZone<\/code> is not an option for <code>DateString<\/code>, I get an error.<\/p>\n<blockquote>\n<p>&#8220;DateString::optx: Unknown option ShowZone in DateString&#8230;&#8221;<\/p>\n<\/blockquote>\n<p>But I can&#8217;t just associate <code>TimeZone<\/code> as an up value for <code>zoned<\/code> with<\/p>\n<pre><code>zoned\/:Options[DateString]={ShowZone-&gt;True}\n<\/code><\/pre>\n<p>because that also results in an error:<\/p>\n<blockquote>\n<p>Options::tag: Rule for Options of Options[DateString] can only be attached to DateString.<\/p>\n<\/blockquote>\n<p>It seems I have to (unprotect <code>DateString<\/code> and) then add a new option globally, which is generally just ignored:<\/p>\n<pre><code>Unprotect[DateString]; \nOptions[DateString] = Union[Options[DateString], {ShowZone -&gt; False}]; \nProtect[DateString];\n<\/code><\/pre>\n<p>Is this the only approach? Even this approach leaves <code>ShowZone<\/code> blue in the function definition and red in function invocations.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 21:07:38. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I&#8217;d like to add an option to a built-in function that only applies when it is given an argument of a certain form. For example, something like this approach to extending DateString to support an extra option when it is given a date argument that matches the form zoned[&#8230;] (forget that zoned does nothing here): [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-613","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/613","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=613"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/613\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=613"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=613"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=613"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}