{"id":107380,"date":"2022-11-10T07:00:00","date_gmt":"2022-11-10T15:00:00","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/oldnewthing\/?p=107380"},"modified":"2022-11-10T06:54:58","modified_gmt":"2022-11-10T14:54:58","slug":"20221110-00","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/oldnewthing\/20221110-00\/?p=107380","title":{"rendered":"On the dangers of giving a product feature the name &#8220;new&#8221;"},"content":{"rendered":"<p>A customer was having trouble adding a default parameter to their Windows Runtime method. When they used the <code>[defaultvalue]<\/code> attribute, they got the error<\/p>\n<pre style=\"white-space: pre-wrap;\">error MIDL2167: [msg]this attribute only allowed with new format type libraries [context]: [defaultvalue()]\r\n<\/pre>\n<p>&#8220;Why does the error tell me that it&#8217;s allowed only with the new thing, when I&#8217;m using the Windows Runtime, which is the latest thing?&#8221;<\/p>\n<p>This is a case of an error message written to a point in time.<\/p>\n<p>Windows 2000 introduced a new type library file format, and you could select whether you wanted the old format or new format by passing the <code>\/oldtlb<\/code> or <code>\/newtlb<\/code> command line switches. The error message is saying that the <code>[defaultvalue]<\/code> attribute is supported only with new-style TLBs.<\/p>\n<p>Mind you, those &#8220;new&#8221; TLBs are now over 20 years old. They may have been new once, but they&#8217;re not new any more.<\/p>\n<p>I&#8217;m not a fan of giving a product feature the name &#8220;new&#8221;. It may be new at the time you introduce it, but after a while, it won&#8217;t be new, and any messages that refer to it as &#8220;new&#8221; will only end up confusing.\u00b9<\/p>\n<p>For example, the <a href=\"https:\/\/en.wikipedia.org\/wiki\/New_Executable\"> New Executable<\/a> format is now over 35 years old, originally designed for 16-bit Windows. Like anybody even remembers that. It&#8217;s so new that it isn&#8217;t supported any more.<\/p>\n<p>Instead of calling your thing &#8220;new&#8221;, name it after the actual new thing. The &#8220;New Executable&#8221; could have been called a &#8220;Segmented Executable&#8221;, for example. If you can&#8217;t find a name for the new thing, you can chicken out and call it &#8220;v2&#8221;. In the above case, the error message would at least say &#8220;This attribute is allowed only with version 2 type libraries.&#8221;<\/p>\n<p>In the case the customer was running into, they were compiling a Windows Runtime interface, and the <code>[defaultvalue]<\/code> attribute is simply not supported by the Windows Runtime at all. It&#8217;s not listed in the <a href=\"https:\/\/docs.microsoft.com\/en-us\/uwp\/midl-3\/\"> documentation for MIDL 3.0<\/a> as a supported keyword.<\/p>\n<p>The Windows Runtime doesn&#8217;t support default parameters because not all languages supported by the Windows Runtime have the concept of default parameters. C++ and C# have them, but JavaScript for example does not, or at least it didn&#8217;t have them until ES2015. Fortunately, you don&#8217;t need default parameters. You can get the same effect with overloads.<\/p>\n<pre>runtimeclass Widget\r\n{\r\n    \/\/ can't do this:\r\n    \/\/ void Sparkle(SparkleOptions options = SparkleOptions.None);\r\n\r\n    \/\/ Do this instead:\r\n    void Sparkle();\r\n    void Sparkle(SparkleOptions options);\r\n}\r\n<\/pre>\n<p>The difference between default parameters and overloads is that default parameters are encoded at the call site and therefore are immutable. On the other hand, the 0-arity overloaded function is implemented by the Widget itself, and it can choose what the default options are, even changing the defaults from version to version.<\/p>\n<p>\u00b9 One of my colleagues told me that when a new shopping mall was built in his home town, the locals called the original one the &#8220;old mall&#8221; and the new one the &#8220;new mall&#8221;. These nicknames stuck even after the so-called &#8220;new mall&#8221; was no longer particularly new. What made things even more confusing is that the &#8220;old mall&#8221; was renovated, but the locals still call it the &#8220;old mall&#8221;, leading to the odd situation where the &#8220;old mall&#8221; is actually newer than the &#8220;new mall&#8221;.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It may be new today, but it won&#8217;t be new tomorrow.<\/p>\n","protected":false},"author":1069,"featured_media":111744,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[25],"class_list":["post-107380","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-oldnewthing","tag-code"],"acf":[],"blog_post_summary":"<p>It may be new today, but it won&#8217;t be new tomorrow.<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107380","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/users\/1069"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/comments?post=107380"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/posts\/107380\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media\/111744"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/media?parent=107380"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/categories?post=107380"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/oldnewthing\/wp-json\/wp\/v2\/tags?post=107380"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}