{"id":11021,"date":"2022-09-25T21:05:59","date_gmt":"2022-09-25T15:35:59","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/"},"modified":"2022-09-25T21:05:59","modified_gmt":"2022-09-25T15:35:59","slug":"solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/","title":{"rendered":"[Solved] Why does the absence of an else block translate to Unit type return for a function?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-58259448\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"58259448\" data-parentid=\"58258897\" data-score=\"4\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<blockquote>\n<p>I can correct this by adding the <code>else<\/code> clause, but how come if there is no outcome handled by default, the function tries to return a <code>Unit<\/code>?<\/p>\n<\/blockquote>\n<p>In Scala, unlike more &#8220;imperative&#8221; languages, (almost) everything is an expression (there are very few statements), and every expression evaluates to a value (which also means that every method returns a value).<\/p>\n<p>This means that, for example, the conditional expression <code>if (condition) consequence else differentConsequence<\/code> is an expression that evaluates to a value.<\/p>\n<p>For example, in this piece of code:<\/p>\n<pre><code>val foo = if (someRandomCondition) 42 else \"Hello\"\n<\/code><\/pre>\n<p>the <code>then<\/code> part of the expression will evaluate to <code>42<\/code>, the <code>else<\/code> part of the expression will evaluate to <code>\"Hello\"<\/code>, which means the <code>if<\/code> expression as a whole will evaluate to either <code>42<\/code> or <code>\"Hello\"<\/code>.<\/p>\n<p>So, what is the type of <code>foo<\/code> going to be? Well, in the <code>then<\/code> case, the value is of type <code>Int<\/code> and in the <code>else<\/code> case, the value is of type <code>String<\/code>. But, this depends on the <em>runtime value<\/em> of <code>someRandomCondition<\/code>, which is unknown at compile time. So, the only choice we have as the type for the <em>whole<\/em> <code>if<\/code> expression is the lowest common ancestor (technically, the <a rel=\"nofollow noopener\" target=\"_blank\" href=\"https:\/\/scala-lang.org\/files\/archive\/spec\/2.13\/06-expressions.html#conditional-expressions\"><em>weak least upper bound<\/em><\/a>) of <code>Int<\/code> and <code>String<\/code>, which is <code>Any<\/code>.<\/p>\n<p>In a language with union types, we could give it a more precise type, namely the union type <code>Int | String<\/code>. (<a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/dotty.epfl.ch\/docs\/reference\/new-types\/union-types.html\">Scala 3 has union types<\/a>, so we could give the expression this exact type, although Scala 3 will not infer union types.) In Scala 3, we could even annotate it with the even more precise type <code>42 | \"Hello\"<\/code>, which is actually the type that TypeScript is going to infer for the equivalent conditional expression:<\/p>\n<pre class=\"lang-js prettyprint-override\"><code>const foo = someRandomCondition ? 42 : \"Hello\"\n<\/code><\/pre>\n<p>Now, let&#8217;s move forward towards the code in the question:<\/p>\n<pre><code>val bar = if (someRandomCondition) 42\n<\/code><\/pre>\n<p>What is the type of <code>bar<\/code> going to be? We said above that it is the lowest common ancestor of the types of the <code>then<\/code> and <code>else<\/code> branch, but \u2026\u00a0what is the type of the <code>else<\/code> branch? What does the <code>else<\/code> branch evaluate to?<\/p>\n<p>Remember, we said that <em>every expression evaluates to a value<\/em>, so the <code>else<\/code> branch <em>must<\/em> evaluate to some value. It can&#8217;t just evaluate to &#8220;nothing&#8221;.<\/p>\n<p>This is solved by a so-called <em>unit value<\/em> of a <em>unit type<\/em>. The unit value and type are called the &#8220;unit&#8221; value and type, because the type is designed in such a way that it <em>can only possibly be inhabited by a single value<\/em>. The unit type has no members, no properties, no fields, no semantics, no nothing. As such, it is impossible to distinguish two values of the unit type from one another, or put another way: there can only be one value of the unit type, because very other value of the unit type <em>must<\/em> be identical.<\/p>\n<p>In many programming languages, the unit value and type use the same notation as a tuple value and type, and are simply identified with the empty tuple <code>()<\/code>. An empty tuple and a unit value are the same thing: they have no content, no meaning. In Haskell, for example, both the type and the value are written <code>()<\/code>.<\/p>\n<p>Scala also has a unit value, and it is also written <code>()<\/code>. The unit type, however, is <code>scala.Unit<\/code>.<\/p>\n<p>So, the unit value, which is a useless value, is used to signify a meaningless return value.<\/p>\n<p>A related, but different concept in some imperative languages is the <code>void<\/code> type (or in some languages, it is more a &#8220;pseudo-type&#8221;).<\/p>\n<p>Note that &#8220;returns nothing&#8221; is different from &#8220;doesn&#8217;t return&#8221;, which will become important in the second part of this answer.<\/p>\n<p>So the first half of the puzzle is: the Scala Language Specification says that <\/p>\n<pre><code>if (condition) expression\n<\/code><\/pre>\n<p>is equivalent to <\/p>\n<pre><code>if (condition) expression else ()\n<\/code><\/pre>\n<p>Which means that in the (implicit) <code>else<\/code> case, the return type is <code>Unit<\/code>, which is not compatible with <code>List[(Int, Int)]<\/code>, and therefore, you get a type error.<\/p>\n<p>But why does throwing an exception fix this?<\/p>\n<p>This brings us to the <em>second<\/em> special type: <code>Nothing<\/code>. <code>Nothing<\/code> is a so-called <em>bottom type<\/em>, which means that it is a <em>subtype of every type<\/em>. <code>Nothing<\/code> does <em>not<\/em> have any value. So, what then, would a return type of <code>Nothing<\/code> signify?<\/p>\n<p>It signifies an expression that <em>doesn&#8217;t return<\/em>. And I repeat what I said above: this is <em>different<\/em> from returning nothing.<\/p>\n<p>A method that has only a side-effect returns nothing, but <em>it does return<\/em>. Its return type is <code>Unit<\/code> and its return value is <code>()<\/code>. It doesn&#8217;t have a <em>meaningful<\/em> return value.<br \/>\nA method that has an infinite loop or throws an exception <em>doesn&#8217;t return at all<\/em>. Its return type is <code>Nothing<\/code> and <em>it doesn&#8217;t have a return value<\/em>.<\/p>\n<p>And that is why throwing an exception in the <code>else<\/code> clause fixes the problem: this means that the type of the <code>else<\/code> clause is <code>Nothing<\/code>, and since <code>Nothing<\/code> is a subtype of <em>every type<\/em>, it doesn&#8217;t even matter <em>what<\/em> the type of the <code>then<\/code> clause is, the lowest common supertype of the type of the <code>then<\/code> clause and <code>Nothing<\/code> will <em>always<\/em> be the type of the <code>then<\/code> clause. (Think about it: the lowest common ancestor of a father and any of his children, grandchildren, great-grandchildren, etc. will always be the father himself. The lowest common ancestor of <code>T<\/code> and any subtype of <code>T<\/code> will always be <code>T<\/code>. Since <code>Nothing<\/code> is a subtype of all types, the lowest common ancestor of <code>T<\/code> and <code>Nothing<\/code> will always be <code>T<\/code> because <code>Nothing<\/code> is always a subtype of <code>T<\/code>, no matter what <code>T<\/code> is.)<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved Why does the absence of an else block translate to Unit type return for a function? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] I can correct this by adding the else clause, but how come if there is no outcome handled by default, the function tries to return a Unit? In Scala, unlike more &#8220;imperative&#8221; languages, (almost) everything is an expression (there are very few statements), and every expression evaluates to a value (which also means that &#8230; <a title=\"[Solved] Why does the absence of an else block translate to Unit type return for a function?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/\" aria-label=\"More on [Solved] Why does the absence of an else block translate to Unit type return for a function?\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[1206,494,802,3025],"class_list":["post-11021","post","type-post","status-publish","format-standard","hentry","category-solved","tag-functional-programming","tag-recursion","tag-scala","tag-tail-recursion"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Why does the absence of an else block translate to Unit type return for a function? - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Why does the absence of an else block translate to Unit type return for a function? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] I can correct this by adding the else clause, but how come if there is no outcome handled by default, the function tries to return a Unit? In Scala, unlike more &#8220;imperative&#8221; languages, (almost) everything is an expression (there are very few statements), and every expression evaluates to a value (which also means that ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-25T15:35:59+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Why does the absence of an else block translate to Unit type return for a function?\",\"datePublished\":\"2022-09-25T15:35:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/\"},\"wordCount\":872,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"functional-programming\",\"recursion\",\"scala\",\"tail-recursion\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/\",\"name\":\"[Solved] Why does the absence of an else block translate to Unit type return for a function? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-25T15:35:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Why does the absence of an else block translate to Unit type return for a function?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] Why does the absence of an else block translate to Unit type return for a function? - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Why does the absence of an else block translate to Unit type return for a function? - JassWeb","og_description":"[ad_1] I can correct this by adding the else clause, but how come if there is no outcome handled by default, the function tries to return a Unit? In Scala, unlike more &#8220;imperative&#8221; languages, (almost) everything is an expression (there are very few statements), and every expression evaluates to a value (which also means that ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/","og_site_name":"JassWeb","article_published_time":"2022-09-25T15:35:59+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Why does the absence of an else block translate to Unit type return for a function?","datePublished":"2022-09-25T15:35:59+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/"},"wordCount":872,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["functional-programming","recursion","scala","tail-recursion"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/","url":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/","name":"[Solved] Why does the absence of an else block translate to Unit type return for a function? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-25T15:35:59+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-why-does-the-absence-of-an-else-block-translate-to-unit-type-return-for-a-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Why does the absence of an else block translate to Unit type return for a function?"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11021","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=11021"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11021\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=11021"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=11021"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=11021"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}