{"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":"2026-10-14T07:52:33","modified_gmt":"2026-10-14T06:52:33","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> 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.<\/p>\n<p>The example I was given was wanting to do:<\/p>\n<pre lang=\"Java\">int foo, bar, baz; list(foo, bar, bar) = func(ponies);\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\">\/** OUTPUT\nFoo = 1\nBar = 2\nBingo = Badgers\nBaz = 3\n**\/\n\npackage scratchpad;\n\nimport static scratchpad.$.$;\nimport static scratchpad.$.$;\nimport static scratchpad.$.expand;\nimport static scratchpad.\u00a3.\u00a3;\n\npublic class MultipleReturn\n{\n\tpublic static void main(String... args)\n\t{\n\t\tnew MultipleReturn().demo();\n\t}\n\n\tpublic void demo()\n\t{\n\t\t\u00a3 foo = \u00a3(-1), bar = \u00a3(-1), baz = \u00a3(-1);\n\t\t\u00a3 bingo = \u00a3(\"\");\n\t\t\n\t\texpand(foo()).into(foo,bar,bingo,baz);\n\n\t\tSystem.out.println(\"Foo = \" + \u00a3(foo));\n\t\tSystem.out.println(\"Bar = \" + \u00a3(bar));\n\t\tSystem.out.println(\"Bingo = \" + \u00a3(bingo));\n\t\tSystem.out.println(\"Baz = \" + \u00a3(baz));\n\t}\n\n\tpublic $&lt;integer,$&lt;integer,$&lt;string,$&lt;integer,$&gt;&gt;&gt;&gt; foo()\n\t{\n\t\treturn $(1,$(2,$(\"Badgers\",$(3,$))));\n\t}\n}\n\ninterface Tuple {}\n\nclass $ implements Tuple\n{\n\tprivate $() { }\n\tpublic static $ $ = new $();\n}\n\nclass $&lt;t,u extends=\"\" tuple=\"\"&gt; implements Tuple\n{\n\tpublic T _1;\n\tpublic U _2;\n\n\tprotected $(T t,U u)\n\t{\n\t\tthis._1 = t;\n\t\tthis._2 = u;\n\t}\n\n\tpublic static  $&lt;t,$&gt; $(T t)\n\t{\n\t\treturn new $&lt;t,$&gt;(t,$);\n\t}\n\n\tpublic static &lt;t,u extends=\"\" tuple=\"\"&gt; $&lt;t,u&gt; $(T t, U u)\n\t{\n\t\treturn new $&lt;t,u&gt;(t,u);\n\t}\n\n\tpublic interface Expander\n\t{\n\t\tpublic void into(\u00a3... refs);\n\t}\n\n\tpublic static Expander expand(final $<!--?,? extends Tuple--> vals)\n\t{\n\t\treturn new Expander()\n\t\t{\n\t\t\tpublic void into(scratchpad.\u00a3... refs)\n\t\t\t{\n\t\t\t\tif (refs.length &lt; 1)\n\t\t\t\t\treturn;\n\n\t\t\t\t$ current = vals;\n\t\t\t\trefs[0]._1 = current._1;\n\t\t\t\tif (current._2 instanceof $)\n\t\t\t\t\treturn;\n\t\t\t\tint i = 1;\n\t\t\t\twhile (!(current._2 instanceof $))\n\t\t\t\t{\n\t\t\t\t\tif (i &gt;= refs.length)\n\t\t\t\t\t\treturn;\n\t\t\t\t\tcurrent = ($)current._2;\n\t\t\t\t\trefs[i]._1 = current._1;\n\t\t\t\t\ti++;\n\t\t\t\t}\n\t\t\t}\n\n\t\t};\n\t\t\n\t}\n}\n\nclass \u00a3 extends $&lt;t,$&gt;\n{\n\tprotected \u00a3(T t)\n\t{\n\t\tsuper(t,$);\n\t}\n\n\tpublic static  \u00a3 \u00a3(T t)\n\t{\n\t\treturn new \u00a3(t);\n\t}\n\n\tpublic static  T \u00a3(\u00a3 t)\n\t{\n\t\treturn t._1;\n\t}\n}\n&lt;\/t,$&gt;&lt;\/t,u&gt;&lt;\/t,u&gt;&lt;\/t,u&gt;&lt;\/t,$&gt;&lt;\/t,$&gt;&lt;\/t,u&gt;&lt;\/integer,$&lt;integer,$&lt;string,$&lt;integer,$&gt;<\/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 property=\"article:modified_time\" content=\"2026-10-14T06:52:33+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\":\"2026-10-14T06:52:33+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":10,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions"}],"predecessor-version":[{"id":2736,"href":"https:\/\/benjiweber.co.uk\/blog\/wp-json\/wp\/v2\/posts\/72\/revisions\/2736"}],"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}]}}