{"id":2191,"date":"2012-01-12T00:26:27","date_gmt":"2012-01-11T23:26:27","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=2191"},"modified":"2021-03-11T09:14:43","modified_gmt":"2021-03-11T08:14:43","slug":"annoying-void-in-c-java-alike","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2012\/01\/annoying-void-in-c-java-alike\/","title":{"rendered":"Annoying Void in C# \/ Java \/ alike"},"content":{"rendered":"<p>One thing which regularly annoys me in C# and Java is the void type. Basically the void type is not a subtype of object, which makes it giant. Now when you write something which accepts a function as an argument you need two version: A version which accepts a function which returns something and a version which accepts a \u2018void-returning\u2019-function (the Action delegates in C#).<\/p>\n<div id=\"attachment_2195\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/01\/void-oh-void.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-2195\" class=\"size-medium wp-image-2195\" title=\"void-oh-void\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/01\/void-oh-void-300x187.png\" alt=\"Void Is Not Cool Enough\" width=\"300\" height=\"187\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/01\/void-oh-void-300x187.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2012\/01\/void-oh-void.png 750w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-2195\" class=\"wp-caption-text\">Void Is Not Cool Enough <\/p><\/div>\n<p>In my code I often end up doing something like this:<\/p>\n<script src=\"https:\/\/gist.github.com\/1597380.js?file=InCSharp.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">class Example\n{\n\t\/\/ Stuff like transactions, run in contexts, binding methods etc etc.\n\tpublic T WithSomeContext&lt;T&gt;(Func&lt;T&gt; runInContext)\n\t{\n\t\t\/\/ do magic and stuff\n\t\tvar result = runInContext();\n\t\t\/\/ do other stuff\n\t\treturn result;\n\t}\n\n\t\/\/ Doublicate to also accept Void returns\n\tpublic void WithSomeContext(Action runInContext)\n\t{\n\t\tWithSomeContext&lt;object&gt;(() =&gt;\n\t\t\t{\n\t\t\t\trunInContext();\n\t\t\t\treturn null;\n\t\t\t});\n\t}\n\n\tpublic void UseIt()\n\t{\n\t\tvar valueOne = 1;\n\t\tvar valueTwo = 2;\n\t\t\/\/ with return value\n\t\tvar sum = WithSomeContext(() =&gt; valueOne + valueTwo);\n\n\t\tWithSomeContext(() =&gt; Console.WriteLine(&quot;Hi-There&quot;));\n\t}\n}<\/code><\/pre><\/noscript>\n<p>That\u2019s just boilerplate which I really don\u2019t need. In many other languages, for example Scala, this is handled more elegantly. The void-type (often called Unit) is a regular type. And void\/unit values just represent \u2018void\/unit\u2018:<\/p>\n<script src=\"https:\/\/gist.github.com\/1597380.js?file=InScala.scala\"><\/script><noscript><pre><code class=\"language-scala scala\">class Example{\n\n  def withContext[T](runInContext:()=&gt;T):T ={\n            \/\/ do magic and stuff\n            val result = runInContext();\n            \/\/ do other stuff\n            return result;\n  }\n  \n  \/\/ no need for another method. \n  \/\/ Our withContext also can accept &#039;void&#039;-returns aka Unit \n\n  def useIt() {\n    val valueOne = 1;\n    val valueTwo = 2;\n    \n    var sum = withContext(() =&gt; valueOne + valueTwo);\n\n    withContext(() =&gt; println(&quot;Hi-There&quot;));\n\n  }\n}<\/code><\/pre><\/noscript>\n<p>Yes, I know, it\u2019s not the end of the (programming) world. There are tons of other issues which lead to more boiler-plate. Anyhow, it is such a little thing and still so god damn annoying. God help us that no new programming includes the same little weakness =).<\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing which regularly annoys me in C# and Java is the void type. Basically the void type is not a subtype of object, which makes it giant. Now when&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[126,15],"tags":[24,295,226],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/2191"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=2191"}],"version-history":[{"count":6,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/2191\/revisions"}],"predecessor-version":[{"id":2198,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/2191\/revisions\/2198"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=2191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=2191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=2191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}