{"id":1211,"date":"2021-07-06T17:58:00","date_gmt":"2021-07-06T23:58:00","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=1211"},"modified":"2021-07-02T18:12:58","modified_gmt":"2021-07-03T00:12:58","slug":"when-extension-methods-arent-a-suitable-replacement","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2021\/07\/06\/when-extension-methods-arent-a-suitable-replacement\/","title":{"rendered":"When extension methods aren&#8217;t a suitable replacement"},"content":{"rendered":"\n<p>A lot of API 2 is extension methods that are replacements for the classic global framework methods.<\/p>\n\n\n\n<p>String.LowerCase is for all intents and purposes the equivalent of Lowercase(string).<\/p>\n\n\n\n<p>Except when its not.<\/p>\n\n\n\n<p>If you happen to have methods that can return a wide variety of types you may not be able to use the extension methods. <\/p>\n\n\n\n<p>For instance if you have something like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function Foo(NameOfTypeToReturn as string) as Variant\n     \/\/ this code IS just for illustration purposes !\n     select case nameOfTypeToReturn\n       case \"String\"\n           return \"string\"\n       case \"Double\"\n           return 1.234\n       case \"Integer\"\n           return 99 \/\/ yeah that many red balloons !\n       else\n           return nil\n       end if\nEnd Function<\/code><\/pre>\n\n\n\n<p>and you were to try and do<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim s As String\n\ns = Foo(\"Double\").Lowercase\n\ns = Lowercase(Foo(\"Double\"))\n<\/code><\/pre>\n\n\n\n<p>only one of these will compile. It&#8217;s NOT the extension method style.<\/p>\n\n\n\n<p>And it makes perfect sense why not.<\/p>\n\n\n\n<p>An extension method has a specific signature.<\/p>\n\n\n\n<p>Lowercase is defined like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Function Lowercase(extends str as string) as String\n<\/code><\/pre>\n\n\n\n<p>essentially for this to MATCH the parameter to it has to already BE a string. But the return value from Foo is a variant &#8211; that can be turned into a string &#8211; but isnt already a string. So that usage &#8220;doesnt match&#8221;. An dyou get a compilation error.<\/p>\n\n\n\n<p>The second however will accept something that can be turned into a string. Which a variant can do. So it compiles just fine.<\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>A lot of API 2 is extension methods that are replacements for the classic global framework methods. String.LowerCase is for all intents and purposes the equivalent of Lowercase(string). Except when its not. If you happen to have methods that can return a wide variety of types you may not be able to use the extension &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2021\/07\/06\/when-extension-methods-arent-a-suitable-replacement\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;When extension methods aren&#8217;t a suitable replacement&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1211","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1211","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/comments?post=1211"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions"}],"predecessor-version":[{"id":1212,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1211\/revisions\/1212"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=1211"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=1211"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=1211"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}