{"id":1299,"date":"2021-12-14T15:54:43","date_gmt":"2021-12-14T22:54:43","guid":{"rendered":"https:\/\/www.great-white-software.com\/blog\/?p=1299"},"modified":"2021-12-14T15:54:43","modified_gmt":"2021-12-14T22:54:43","slug":"multiple-return-types-dream","status":"publish","type":"post","link":"https:\/\/www.great-white-software.com\/blog\/2021\/12\/14\/multiple-return-types-dream\/","title":{"rendered":"Multiple return types dream"},"content":{"rendered":"\n<p>Suppose you want to do something  like a set of preferences that are a simple key\/value pairing.<\/p>\n\n\n\n<p>A dictionary seems a great choice. Its keys can be any type and the values any type because both are variants.<\/p>\n\n\n\n<p>So far so good.<\/p>\n\n\n\n<p>Now you can do something like<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim i as integer = PrefsDict.Value(\"keyForIntegerValuedPreference\")<\/code><\/pre>\n\n\n\n<p>And all seems great ! i gets an integer value and life is good<\/p>\n\n\n\n<p>Except that this is ALSO legitimate<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim s as string = PrefsDict.Value(\"keyForIntegerValuedPreference\")<\/code><\/pre>\n\n\n\n<p>And now your &#8220;strongly typed&#8221; system is fubarred. And the Xojo compiler will  not complain<\/p>\n\n\n\n<p>So how are you going to get an integer from something that SHOULD be an integer, and a string from something that should be a string ?<br>And get the compiler to help you do this ?<\/p>\n\n\n\n<p>If you just use a dictionary you wont be able to. Its accessors dont have that capability. <\/p>\n\n\n\n<p>You could try<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GetValue(extends d as dictionary, key as variant) as Integer\nGetValue(extends d as dictionary, key as variant) as String<\/code><\/pre>\n\n\n\n<p>but it wont work. Xojo doesn&#8217;t support multiple methods with the same signature and only differeing in return type \ud83d\ude41<\/p>\n\n\n\n<p>So how could you do this AND get the compiler to help you out ? What if instead of return values (which I will admit would be nicer) you do<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>GetValue(extends d as dictionary, key as variant, byref value as Integer)\nGetValue(extends d as dictionary, key as variant, byref value as String)<\/code><\/pre>\n\n\n\n<p>and so on for every type ?<\/p>\n\n\n\n<p>Now the compiler will tell you when you try to do something unsupported. <br>In our example with only integer and string values supported if we did<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>dim d as double\nPrefsDict.GetValue(\"keyForIntegerValuedPreference\", d)<\/code><\/pre>\n\n\n\n<p>the compiler would tell us this isnt supported<\/p>\n\n\n\n<p>Its about the best we can hope for until\/unless Xojo alters the compiler to permit several return types (and does the correct work to make use of it) In theory Xojo could make it so the compiler matched the right signature with the right return type to whatever is expected (like the following)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>Dim i as integer = PrefsDict.GetValue(\"keyForIntegerValuedPreference\") \/\/ would call the integer returning version\ndim s as string = PrefsDict.GetValue(\"keyForStringValuedPreference\") \/\/ would call the String returning version<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Suppose you want to do something like a set of preferences that are a simple key\/value pairing. A dictionary seems a great choice. Its keys can be any type and the values any type because both are variants. So far so good. Now you can do something like And all seems great ! i gets &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/www.great-white-software.com\/blog\/2021\/12\/14\/multiple-return-types-dream\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Multiple return types dream&#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-1299","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\/1299","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=1299"}],"version-history":[{"count":1,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1299\/revisions"}],"predecessor-version":[{"id":1300,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/posts\/1299\/revisions\/1300"}],"wp:attachment":[{"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/media?parent=1299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/categories?post=1299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.great-white-software.com\/blog\/wp-json\/wp\/v2\/tags?post=1299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}