{"id":72,"date":"2009-06-18T18:26:12","date_gmt":"2009-06-18T17:26:12","guid":{"rendered":"http:\/\/benjiweber.co.uk\/blog\/?p=72"},"modified":"2009-06-18T18:26:12","modified_gmt":"2009-06-18T17:26:12","slug":"java-abuse-currency-pattern","status":"publish","type":"post","link":"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/18\/java-abuse-currency-pattern\/","title":{"rendered":"Java Abuse &#8211; Currency Pattern"},"content":{"rendered":"<p class=\"lead\">Here&#8217;s a sillier one from last night&#8230;<\/p>\n<p>People <a href=\"http:\/\/bugs.sun.com\/bugdatabase\/view_bug.do?bug_id=4222792\">often<\/a> <a href=\"http:\/\/www.javalobby.org\/java\/forums\/t104018.html\">complain<\/a> about not being able to return multiple values from a method in Java. I can&#8217;t see a good reason for wanting to do this, but some do.<\/p>\n<p>The example I was given was wanting to do:<\/p>\n<pre lang=\"Java\">\r\nint foo, bar, baz; list(foo, bar, bar) = func(ponies);\r\n<\/pre>\n<p>Where func(ponies) returns 3 values which are then assigned to the local variables foo, bar, and baz.<\/p>\n<p>There are plenty of sensible ways of achieving this, but what if we&#8217;re trying to get as close as possible to the above method? We can only return one thing from a method, but that could be a chain. We also can&#8217;t modify the local foo,bar,baz from inside the list method, but we could if we wrap them in references.<\/p>\n<p>If we abuse the fact that <a href=\"http:\/\/java.sun.com\/j2se\/1.4.2\/docs\/api\/java\/lang\/Character.html#isJavaLetter(char)\">currency characters are valid identifiers in Java<\/a>, for the ultimate in unreadability; We can use $(x,y) to be a pair of x and y, $$ to terminate a list, and \u00a3(x) to either reference or dereference x.<\/p>\n<p>Then we can do something like this \u00ac_\u00ac<\/p>\n<pre lang=\"Java\">\r\n\/** OUTPUT\r\nFoo = 1\r\nBar = 2\r\nBingo = Badgers\r\nBaz = 3\r\n**\/\r\n\r\npackage scratchpad;\r\n\r\nimport static scratchpad.$.$;\r\nimport static scratchpad.$$.$$;\r\nimport static scratchpad.$.expand;\r\nimport static scratchpad.\u00a3.\u00a3;\r\n\r\npublic class MultipleReturn\r\n{\r\n\tpublic static void main(String... args)\r\n\t{\r\n\t\tnew MultipleReturn().demo();\r\n\t}\r\n\r\n\tpublic void demo()\r\n\t{\r\n\t\t\u00a3<Integer> foo = \u00a3(-1), bar = \u00a3(-1), baz = \u00a3(-1);\r\n\t\t\u00a3<String> bingo = \u00a3(\"\");\r\n\t\t\r\n\t\texpand(foo()).into(foo,bar,bingo,baz);\r\n\r\n\t\tSystem.out.println(\"Foo = \" + \u00a3(foo));\r\n\t\tSystem.out.println(\"Bar = \" + \u00a3(bar));\r\n\t\tSystem.out.println(\"Bingo = \" + \u00a3(bingo));\r\n\t\tSystem.out.println(\"Baz = \" + \u00a3(baz));\r\n\t}\r\n\r\n\tpublic $<Integer,$<Integer,$<String,$<Integer,$$>>>> foo()\r\n\t{\r\n\t\treturn $(1,$(2,$(\"Badgers\",$(3,$$))));\r\n\t}\r\n}\r\n\r\ninterface Tuple {}\r\n\r\nclass $$ implements Tuple\r\n{\r\n\tprivate $$() { }\r\n\tpublic static $$ $$ = new $$();\r\n}\r\n\r\nclass $<T,U extends Tuple> implements Tuple\r\n{\r\n\tpublic T _1;\r\n\tpublic U _2;\r\n\r\n\tprotected $(T t,U u)\r\n\t{\r\n\t\tthis._1 = t;\r\n\t\tthis._2 = u;\r\n\t}\r\n\r\n\tpublic static <T> $<T,$$> $(T t)\r\n\t{\r\n\t\treturn new $<T,$$>(t,$$);\r\n\t}\r\n\r\n\tpublic static <T,U extends Tuple> $<T,U> $(T t, U u)\r\n\t{\r\n\t\treturn new $<T,U>(t,u);\r\n\t}\r\n\r\n\tpublic interface Expander\r\n\t{\r\n\t\tpublic void into(\u00a3... refs);\r\n\t}\r\n\r\n\tpublic static Expander expand(final $<?,? extends Tuple> vals)\r\n\t{\r\n\t\treturn new Expander()\r\n\t\t{\r\n\t\t\tpublic void into(scratchpad.\u00a3... refs)\r\n\t\t\t{\r\n\t\t\t\tif (refs.length < 1)\r\n\t\t\t\t\treturn;\r\n\r\n\t\t\t\t$ current = vals;\r\n\t\t\t\trefs[0]._1 = current._1;\r\n\t\t\t\tif (current._2 instanceof $$)\r\n\t\t\t\t\treturn;\r\n\t\t\t\tint i = 1;\r\n\t\t\t\twhile (!(current._2 instanceof $$))\r\n\t\t\t\t{\r\n\t\t\t\t\tif (i >= refs.length)\r\n\t\t\t\t\t\treturn;\r\n\t\t\t\t\tcurrent = ($)current._2;\r\n\t\t\t\t\trefs[i]._1 = current._1;\r\n\t\t\t\t\ti++;\r\n\t\t\t\t}\r\n\t\t\t}\r\n\r\n\t\t};\r\n\t\t\r\n\t}\r\n}\r\n\r\nclass \u00a3<T> extends $<T,$$>\r\n{\r\n\tprotected \u00a3(T t)\r\n\t{\r\n\t\tsuper(t,$$);\r\n\t}\r\n\r\n\tpublic static <T> \u00a3<T> \u00a3(T t)\r\n\t{\r\n\t\treturn new \u00a3<T>(t);\r\n\t}\r\n\r\n\tpublic static <T> T \u00a3(\u00a3<T> t)\r\n\t{\r\n\t\treturn t._1;\r\n\t}\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here&#8217;s a sillier one from last night&#8230; People often complain about not being able to return multiple values from a method in Java. I can&#8217;t see a good reason for wanting to do this, but some do. The example I was given was wanting to do: int foo, bar, baz; list(foo, bar, bar) = func(ponies);&#8230;  <a href=\"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/18\/java-abuse-currency-pattern\/\" class=\"more-link\" title=\"Read Java Abuse &#8211; Currency Pattern\">Read more &raquo;<\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[8],"tags":[23],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/18\/java-abuse-currency-pattern\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Java Abuse - Currency Pattern - Benji&#039;s Blog\" \/>\n<meta property=\"og:description\" content=\"Here&#8217;s a sillier one from last night&#8230; People often complain about not being able to return multiple values from a method in Java. I can&#8217;t see a good reason for wanting to do this, but some do. The example I was given was wanting to do: int foo, bar, baz; list(foo, bar, bar) = func(ponies);... Read more &raquo;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/18\/java-abuse-currency-pattern\/\" \/>\n<meta property=\"og:site_name\" content=\"Benji&#039;s Blog\" \/>\n<meta property=\"article:published_time\" content=\"2009-06-18T17:26:12+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/\",\"name\":\"Benji&#039;s Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/benjiweber.co.uk\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/18\/java-abuse-currency-pattern\/#webpage\",\"url\":\"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/18\/java-abuse-currency-pattern\/\",\"name\":\"Java Abuse - Currency Pattern - Benji&#039;s Blog\",\"isPartOf\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#website\"},\"datePublished\":\"2009-06-18T17:26:12+00:00\",\"dateModified\":\"2009-06-18T17:26:12+00:00\",\"author\":{\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/benjiweber.co.uk\/blog\/2009\/06\/18\/java-abuse-currency-pattern\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#\/schema\/person\/45ecb36b51f4ce99e6929d2d31ca5c09\",\"name\":\"benji\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/benjiweber.co.uk\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/05fb47b31a0b329e1b790074a9b624ef?s=96&d=mm&r=g\",\"caption\":\"benji\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","amp_enabled":true,"_links":{"self":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/72"}],"collection":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/comments?post=72"}],"version-history":[{"count":9,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":81,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions\/81"}],"wp:attachment":[{"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/media?parent=72"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/categories?post=72"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/tags?post=72"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}