{"id":3969,"date":"2018-04-01T10:12:17","date_gmt":"2018-04-01T08:12:17","guid":{"rendered":"https:\/\/monodes.com\/predaelli\/?p=3969"},"modified":"2018-04-01T10:12:17","modified_gmt":"2018-04-01T08:12:17","slug":"its-time-to-start-using-a-modern-programming-language-eiffel","status":"publish","type":"post","link":"https:\/\/monodes.com\/predaelli\/2018\/04\/01\/its-time-to-start-using-a-modern-programming-language-eiffel\/","title":{"rendered":"It\u2019s time to start using a modern programming language: Eiffel"},"content":{"rendered":"<h1>It\u2019s time to start using a modern programming language: Eiffel<\/h1>\n<p>In fact as I read <em><a href=\"https:\/\/medium.com\/@magnus.chatt\/why-you-should-totally-switch-to-kotlin-c7bbde9e10d5\">Why you should totally switch to Kotlin \u2013 Magnus Vinther \u2013 Medium<\/a><\/em> I wonderded why they haven&#8217;t switched to Eiffel, as it has had for decades most of the features they praised in Kotlin!<\/p>\n<p><!--more--><!--nextpage--><\/p>\n<blockquote>\n<h1 id=\"4913\" class=\"graf graf--h3 graf-after--figure graf--title\">Why you should totally switch to\u00a0Kotlin<\/h1>\n<h2 id=\"e782\" class=\"graf graf--h4 graf-after--h3 graf--subtitle\">It\u2019s time to start using a modern programming language<\/h2>\n<p id=\"c31d\" class=\"graf graf--p graf-after--h4\">I want to tell you about a <strong class=\"markup--strong markup--p-strong\">new programming language<\/strong> called <strong class=\"markup--strong markup--p-strong\">Kotlin<\/strong> and why you should consider it for your next project. I used to prefer Java but the last year I\u2019ve found myself coding Kotlin whenever I could, and at this point I really can\u2019t think of a situation where Java would be a better choice.<\/p>\n<p id=\"75ca\" class=\"graf graf--p graf-after--p\">It\u2019s developed by <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/www.jetbrains.com\/\" target=\"_blank\" rel=\"noopener nofollow\" data-href=\"https:\/\/www.jetbrains.com\/\"><strong class=\"markup--strong markup--p-strong\">JetBrains<\/strong><\/a>, and the fact that these are the people behind a suite of <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/www.jetbrains.com\/products.html?fromMenu#type=ide\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/www.jetbrains.com\/products.html?fromMenu#type=ide\">IDEs<\/a>, such as <strong class=\"markup--strong markup--p-strong\">IntelliJ <\/strong>and <strong class=\"markup--strong markup--p-strong\">ReSharper<\/strong>, really shines through in Kotlin. It\u2019s <strong class=\"markup--strong markup--p-strong\">pragmatic<\/strong> and <strong class=\"markup--strong markup--p-strong\">concise<\/strong>, and makes coding a satisfying and efficient experience.<\/p>\n<p id=\"603b\" class=\"graf graf--p graf-after--p\">Although Kotlin compiles to both <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/tutorials\/javascript\/kotlin-to-javascript\/kotlin-to-javascript.html\" target=\"_blank\" rel=\"noopener nofollow\" data-href=\"https:\/\/kotlinlang.org\/docs\/tutorials\/javascript\/kotlin-to-javascript\/kotlin-to-javascript.html\"><strong class=\"markup--strong markup--p-strong\">JavaScript<\/strong><\/a> and soon <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/blog.jetbrains.com\/kotlin\/2017\/04\/kotlinnative-tech-preview-kotlin-without-a-vm\/\" target=\"_blank\" rel=\"noopener nofollow\" data-href=\"https:\/\/blog.jetbrains.com\/kotlin\/2017\/04\/kotlinnative-tech-preview-kotlin-without-a-vm\/\"><strong class=\"markup--strong markup--p-strong\">machine code<\/strong><\/a>, I\u2019ll focus on its prime environment, the <strong class=\"markup--strong markup--p-strong\">JVM<\/strong>.<\/p>\n<p id=\"4cfb\" class=\"graf graf--p graf-after--p\">So here\u2019s a couple of reasons why you should totally switch to Kotlin (in no particular order):<\/p>\n<h4 id=\"aae5\" class=\"graf graf--h4 graf-after--p\">0# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/java-interop.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/java-interop.html\">Java Interoperability<\/a><\/h4>\n<p id=\"a00f\" class=\"graf graf--p graf-after--h4\">Kotlin is <strong class=\"markup--strong markup--p-strong\">100% interoperable with Java<\/strong>. You can literally continue work on your old Java projects using Kotlin. All your favorite <strong class=\"markup--strong markup--p-strong\">Java frameworks are still available<\/strong>, and whatever framework you\u2019ll write in Kotlin is easily adopted by your stubborn Java loving friend.<\/p>\n<h4 id=\"6215\" class=\"graf graf--h4 graf-after--p\"><strong class=\"markup--strong markup--h4-strong\">1# <\/strong><a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/basic-syntax.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/basic-syntax.html\"><strong class=\"markup--strong markup--h4-strong\">Familiar\u00a0Syntax<\/strong><\/a><\/h4>\n<p id=\"5a75\" class=\"graf graf--p graf-after--h4\">Kotlin isn\u2019t some weird language born in academia. Its syntax is familiar to any programmer coming from the <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming\" target=\"_blank\" rel=\"noopener nofollow\" data-href=\"https:\/\/en.wikipedia.org\/wiki\/Object-oriented_programming\">OOP<\/a> domain, and can be more or less understood from the get go. There are of course <em class=\"markup--em markup--p-em\">some<\/em> differences from Java such as the reworked constructors or the <code class=\"\" data-line=\"\">val<\/code> <code class=\"\" data-line=\"\">var<\/code> variable declarations. The snippet below presents most of the basics:<\/p>\n<pre id=\"b851\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">class<\/strong> Foo {\n\n    <strong class=\"markup--strong markup--pre-strong\">val<\/strong> b: String = \"b\"     <em class=\"markup--em markup--pre-em\">\/\/ <\/em>val means unmodifiable\n    <strong class=\"markup--strong markup--pre-strong\">var<\/strong> i: Int = 0          <em class=\"markup--em markup--pre-em\">\/\/ <\/em>var means modifiable\n\n    <strong class=\"markup--strong markup--pre-strong\">fun<\/strong> hello() {\n        <strong class=\"markup--strong markup--pre-strong\">val<\/strong> str = \"Hello\"\n        <em class=\"markup--em markup--pre-em\">print<\/em>(\"$str World\")\n    }\n\n    <strong class=\"markup--strong markup--pre-strong\">fun<\/strong> sum(x: Int, y: Int): Int {\n        <strong class=\"markup--strong markup--pre-strong\">return<\/strong> x + y\n    }\n\n    <strong class=\"markup--strong markup--pre-strong\">fun<\/strong> maxOf(a: Float, b: Float) = <strong class=\"markup--strong markup--pre-strong\">if<\/strong> (a &gt; b) a <strong class=\"markup--strong markup--pre-strong\">else<\/strong> b\n\n}<\/pre>\n<h4 id=\"47f3\" class=\"graf graf--h4 graf-after--pre\"><strong class=\"markup--strong markup--h4-strong\">2# <\/strong><a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/basic-types.html#string-templates\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/basic-types.html#string-templates\"><strong class=\"markup--strong markup--h4-strong\">String Interpolation<\/strong><\/a><\/h4>\n<p id=\"3a05\" class=\"graf graf--p graf-after--h4\">It\u2019s as if a smarter and more readable version of Java\u2019s <code class=\"\" data-line=\"\">String.format()<\/code> was built into the language:<\/p>\n<pre id=\"8e5e\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">val<\/strong> x = 4\n<strong class=\"markup--strong markup--pre-strong\">val<\/strong> y = 7\n<em class=\"markup--em markup--pre-em\">print<\/em>(\"sum of $x and $y is ${x + y}\")  \/\/ sum of 4 and 7 is 11<\/pre>\n<h4 id=\"5222\" class=\"graf graf--h4 graf-after--pre\">3# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/properties.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/properties.html\">Type Inference<\/a><\/h4>\n<p id=\"db29\" class=\"graf graf--p graf-after--h4\">Kotlin will infer your types wherever you feel it will improve readability:<\/p>\n<pre id=\"57c6\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">val<\/strong> a = \"abc\"                         \/\/ type inferred to String\n<strong class=\"markup--strong markup--pre-strong\">val<\/strong> b = 4                             \/\/ type inferred to Int\n\n<strong class=\"markup--strong markup--pre-strong\">val<\/strong> c: Double = 0.7                   \/\/ type declared explicitly\n<strong class=\"markup--strong markup--pre-strong\">val<\/strong> d: List&lt;String&gt; = ArrayList()     \/\/ type declared explicitly<\/pre>\n<h4 id=\"e81a\" class=\"graf graf--h4 graf-after--pre\">4# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/typecasts.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/typecasts.html\">Smart\u00a0Casts<\/a><\/h4>\n<p id=\"b5f0\" class=\"graf graf--p graf-after--h4\">The Kotlin compiler tracks your logic and <strong class=\"markup--strong markup--p-strong\">auto-casts types if possible<\/strong>, which means no more <code class=\"\" data-line=\"\">instanceof<\/code> checks followed by explicit casts:<\/p>\n<pre id=\"aa3f\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">if<\/strong> (obj <strong class=\"markup--strong markup--pre-strong\">is<\/strong> String) {\n    <em class=\"markup--em markup--pre-em\">print<\/em>(obj.<em class=\"markup--em markup--pre-em\">toUpperCase<\/em>())     \/\/ obj is now known to be a String\n}<\/pre>\n<h4 id=\"b3af\" class=\"graf graf--h4 graf-after--pre\">5# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/equality.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/equality.html\">Intuitive Equals<\/a><\/h4>\n<p id=\"aba8\" class=\"graf graf--p graf-after--h4\">You can stop calling <code class=\"\" data-line=\"\">equals()<\/code> explicitly, because the <code class=\"\" data-line=\"\">==<\/code> operator now checks for structural equality:<\/p>\n<pre id=\"270b\" class=\"graf graf--pre graf-after--p\">val john1 = Person(\"John\")\nval john2 = Person(\"John\")<\/pre>\n<pre id=\"eea9\" class=\"graf graf--pre graf-after--pre\">john1 == john2    \/\/ true  (structural equality)\njohn1 === john2   \/\/ false (referential equality)<\/pre>\n<h4 id=\"df66\" class=\"graf graf--h4 graf-after--pre\">6# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/functions.html#default-arguments\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/functions.html#default-arguments\">Default Arguments<\/a><\/h4>\n<p id=\"2fc3\" class=\"graf graf--p graf-after--h4\">No need to define several similar methods with varying arguments:<\/p>\n<pre id=\"625e\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">fun<\/strong> build(title: String, width: Int = 800, height: Int = 600) {\n    Frame(title, width, height)\n}<\/pre>\n<h4 id=\"ceeb\" class=\"graf graf--h4 graf-after--pre\">7# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/functions.html#named-arguments\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/functions.html#named-arguments\">Named Arguments<\/a><\/h4>\n<p id=\"7371\" class=\"graf graf--p graf-after--h4\">Combined with default arguments, named arguments eliminates the need for <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/en.wikipedia.org\/wiki\/Builder_pattern\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/en.wikipedia.org\/wiki\/Builder_pattern\">builders<\/a>:<\/p>\n<pre id=\"191c\" class=\"graf graf--pre graf-after--p\">build(\"PacMan\", 400, 300)                           \/\/ equivalent\nbuild(title = \"PacMan\", width = 400, height = 300)  \/\/ equivalent\nbuild(width = 400, height = 300, title = \"PacMan\")  \/\/ equivalent<\/pre>\n<h4 id=\"00ad\" class=\"graf graf--h4 graf-after--pre\">8# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/control-flow.html#when-expression\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/control-flow.html#when-expression\">The When Expression<\/a><\/h4>\n<p id=\"42ef\" class=\"graf graf--p graf-after--h4\">The switch case is replaced with the much more readable and flexible <em class=\"markup--em markup--p-em\">when<\/em> expression:<\/p>\n<pre id=\"0890\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">when<\/strong> (x) {\n    1 -&gt; <em class=\"markup--em markup--pre-em\">print<\/em>(\"x is 1\")\n    2 -&gt; <em class=\"markup--em markup--pre-em\">print<\/em>(\"x is 2\")\n    3, 4 -&gt; <em class=\"markup--em markup--pre-em\">print<\/em>(\"x is 3 or 4\")\n    <strong class=\"markup--strong markup--pre-strong\">in<\/strong> 5..10 -&gt; <em class=\"markup--em markup--pre-em\">print<\/em>(\"x is 5, 6, 7, 8, 9, or 10\")\n    <strong class=\"markup--strong markup--pre-strong\">else<\/strong> -&gt; <em class=\"markup--em markup--pre-em\">print<\/em>(\"x is out of range\")\n}<\/pre>\n<p id=\"5739\" class=\"graf graf--p graf-after--pre\">It works both as an expression or a statement, and with or without an argument:<\/p>\n<pre id=\"835e\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">val<\/strong> res: Boolean = <strong class=\"markup--strong markup--pre-strong\">when<\/strong> {\n    obj == <strong class=\"markup--strong markup--pre-strong\">null<\/strong> -&gt; false\n    obj <strong class=\"markup--strong markup--pre-strong\">is<\/strong> String -&gt; true\n    <strong class=\"markup--strong markup--pre-strong\">else<\/strong> -&gt; <strong class=\"markup--strong markup--pre-strong\">throw<\/strong> IllegalStateException()\n}<\/pre>\n<h4 id=\"cdaf\" class=\"graf graf--h4 graf-after--pre\"><strong class=\"markup--strong markup--h4-strong\">9# <\/strong><a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/properties.html#getters-and-setters\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/properties.html#getters-and-setters\"><strong class=\"markup--strong markup--h4-strong\">Properties<\/strong><\/a><\/h4>\n<p id=\"5a2f\" class=\"graf graf--p graf-after--h4\">Custom set &amp; get behavior can be added to public fields, which means we can stop bloating our code with mindless <a class=\"markup--anchor markup--p-anchor\" href=\"http:\/\/stackoverflow.com\/questions\/1568091\/why-use-getters-and-setters\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"http:\/\/stackoverflow.com\/questions\/1568091\/why-use-getters-and-setters\">getters &amp; setters<\/a>.<\/p>\n<pre id=\"ceff\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">class <\/strong>Frame {\n    <strong class=\"markup--strong markup--pre-strong\">var<\/strong> width: Int = 800\n    <strong class=\"markup--strong markup--pre-strong\">var <\/strong>height: Int = 600\n\n    <strong class=\"markup--strong markup--pre-strong\">val <\/strong>pixels: Int\n        <strong class=\"markup--strong markup--pre-strong\">get<\/strong>() = width * height\n}<\/pre>\n<h4 id=\"6c8a\" class=\"graf graf--h4 graf-after--pre\"><strong class=\"markup--strong markup--h4-strong\">10# <\/strong><a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/data-classes.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/data-classes.html\"><strong class=\"markup--strong markup--h4-strong\">The Data\u00a0Class<\/strong><\/a><\/h4>\n<p id=\"ab61\" class=\"graf graf--p graf-after--h4\">It\u2019s a POJO complete with <code class=\"\" data-line=\"\">toString()<\/code>, <code class=\"\" data-line=\"\">equals()<\/code>, <code class=\"\" data-line=\"\">hashCode()<\/code>, and <code class=\"\" data-line=\"\">copy()<\/code>, and unlike in Java it won\u2019t take up 100 lines of code:<\/p>\n<pre id=\"5f3f\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">data class<\/strong> Person(<strong class=\"markup--strong markup--pre-strong\">val<\/strong> name: String,\n                  <strong class=\"markup--strong markup--pre-strong\">var<\/strong> email: String,\n                  <strong class=\"markup--strong markup--pre-strong\">var<\/strong> age: Int)\n\n<strong class=\"markup--strong markup--pre-strong\">val<\/strong> john = Person(\"John\", \"john@gmail.com\", 112)<\/pre>\n<h4 id=\"9294\" class=\"graf graf--h4 graf-after--pre\">11# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/operator-overloading.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/operator-overloading.html\">Operator Overloading<\/a><\/h4>\n<p id=\"c847\" class=\"graf graf--p graf-after--h4\">A predefined set of operators can be overloaded to improve readability:<\/p>\n<pre id=\"31e1\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">data class<\/strong> Vec(<strong class=\"markup--strong markup--pre-strong\">val <\/strong>x: Float, <strong class=\"markup--strong markup--pre-strong\">val <\/strong>y: Float) {\n    <strong class=\"markup--strong markup--pre-strong\">operator fun<\/strong> plus(v: Vec) = Vec(x + v.x, y + v.y)\n}\n\n<strong class=\"markup--strong markup--pre-strong\">val <\/strong>v = Vec(2f, 3f) + Vec(4f, 1f)<\/pre>\n<h4 id=\"5d53\" class=\"graf graf--h4 graf-after--pre\">12# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/multi-declarations.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/multi-declarations.html\">Destructuring Declarations<\/a><\/h4>\n<p id=\"e602\" class=\"graf graf--p graf-after--h4\">Some objects can be destructured, which is for example useful for iterating maps:<\/p>\n<pre id=\"3444\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">for <\/strong>((key, value) <strong class=\"markup--strong markup--pre-strong\">in <\/strong>map) {\n    <em class=\"markup--em markup--pre-em\">print<\/em>(\"Key: $key\")\n    <em class=\"markup--em markup--pre-em\">print<\/em>(\"Value: $value\")\n}<\/pre>\n<h4 id=\"9647\" class=\"graf graf--h4 graf-after--pre\">13# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/ranges.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/ranges.html\">Ranges<\/a><\/h4>\n<p id=\"11fa\" class=\"graf graf--p graf-after--h4\">For readability\u2019s sake:<\/p>\n<pre id=\"b57f\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">for<\/strong> (i <strong class=\"markup--strong markup--pre-strong\">in<\/strong> 1..100) { ... } \n<strong class=\"markup--strong markup--pre-strong\">for<\/strong> (i <strong class=\"markup--strong markup--pre-strong\">in<\/strong> 0 until 100) { ... }\n<strong class=\"markup--strong markup--pre-strong\">for<\/strong> (i <strong class=\"markup--strong markup--pre-strong\">in<\/strong> 2..10 step 2) { ... } \n<strong class=\"markup--strong markup--pre-strong\">for<\/strong> (i <strong class=\"markup--strong markup--pre-strong\">in<\/strong> 10 downTo 1) { ... } \n<strong class=\"markup--strong markup--pre-strong\">if<\/strong> (x <strong class=\"markup--strong markup--pre-strong\">in<\/strong> 1..10) { ... }<\/pre>\n<h4 id=\"bf5d\" class=\"graf graf--h4 graf-after--pre\"><strong class=\"markup--strong markup--h4-strong\">14# <\/strong><a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/extensions.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/extensions.html\"><strong class=\"markup--strong markup--h4-strong\">Extension Functions<\/strong><\/a><\/h4>\n<p id=\"d038\" class=\"graf graf--p graf-after--h4\">Remember the first time you had to sort a <code class=\"\" data-line=\"\">List<\/code> in Java? You couldn\u2019t find a <code class=\"\" data-line=\"\">sort()<\/code>function so you had to ask either your tutor or google to learn of <code class=\"\" data-line=\"\">Collections.sort()<\/code>. And later when you had to capitalize a <code class=\"\" data-line=\"\">String<\/code>, you ended up writing your own helper function because you didn\u2019t know of <code class=\"\" data-line=\"\">StringUtils.capitalize()<\/code>.<\/p>\n<p id=\"035f\" class=\"graf graf--p graf-after--p\">If only there was a way to add new functions to old classes; that way your IDE could help you find the right function in code-completion. In Kotlin you can do exactly that:<\/p>\n<pre id=\"fe13\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">fun<\/strong> String.replaceSpaces(): String {\n    <strong class=\"markup--strong markup--pre-strong\">return<\/strong> this.<em class=\"markup--em markup--pre-em\">replace<\/em>(' ', '_')\n}\n\n<strong class=\"markup--strong markup--pre-strong\">val <\/strong>formatted = str.replaceSpaces()<\/pre>\n<p id=\"5fe8\" class=\"graf graf--p graf-after--pre\">The standard library extends the functionality of Java\u2019s original types, which was especially needed for <code class=\"\" data-line=\"\">String<\/code>:<\/p>\n<pre id=\"5fd2\" class=\"graf graf--pre graf-after--p\">str.<em class=\"markup--em markup--pre-em\">removeSuffix<\/em>(\".txt\")\nstr.<em class=\"markup--em markup--pre-em\">capitalize<\/em>()\nstr.<em class=\"markup--em markup--pre-em\">substringAfterLast<\/em>(\"\/\")\nstr.<em class=\"markup--em markup--pre-em\">replaceAfter<\/em>(\":\", \"classified\")<\/pre>\n<h4 id=\"18a4\" class=\"graf graf--h4 graf-after--pre\"><strong class=\"markup--strong markup--h4-strong\">15# <\/strong><a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/null-safety.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/null-safety.html\"><strong class=\"markup--strong markup--h4-strong\">Null\u00a0Safety<\/strong><\/a><\/h4>\n<p id=\"9347\" class=\"graf graf--p graf-after--h4\"><span class=\"markup--quote markup--p-quote is-other\" data-creator-ids=\"anon c5ee634ce079\">Java is what we should call an <em class=\"markup--em markup--p-em\">almost<\/em> statically typed language. In it, a variable of type <code class=\"\" data-line=\"\">String<\/code> is not <em class=\"markup--em markup--p-em\">guaranteed<\/em> to refer to a <code class=\"\" data-line=\"\">String<\/code>\u2014 it might refer to <code class=\"\" data-line=\"\">null<\/code>.<\/span> Even though we are used to this, it negates the safety of static type checking, and as a result Java developers have to live in constant fear of <a class=\"markup--anchor markup--p-anchor\" href=\"http:\/\/stackoverflow.com\/questions\/218384\/what-is-a-nullpointerexception-and-how-do-i-fix-it\" target=\"_blank\" rel=\"nofollow noopener noopener\" data-href=\"http:\/\/stackoverflow.com\/questions\/218384\/what-is-a-nullpointerexception-and-how-do-i-fix-it\">NPEs<\/a>.<\/p>\n<p id=\"f043\" class=\"graf graf--p graf-after--p\">Kotlin resolves this by distinguishing between <strong class=\"markup--strong markup--p-strong\">non-null types<\/strong> and <strong class=\"markup--strong markup--p-strong\">nullable types<\/strong>. Types are non-null by default, and can be made nullable by adding a\u00a0<code class=\"\" data-line=\"\">?<\/code>like so:<\/p>\n<pre id=\"0403\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">var<\/strong> a: String = \"abc\"\na = <strong class=\"markup--strong markup--pre-strong\">null<\/strong>                \/\/ compile error\n\n<strong class=\"markup--strong markup--pre-strong\">var<\/strong> b: String? = \"xyz\"\nb = <strong class=\"markup--strong markup--pre-strong\">null<\/strong>                \/\/ no problem<\/pre>\n<p id=\"91cb\" class=\"graf graf--p graf-after--pre\">Kotlin forces you to guard against NPEs whenever you access a nullable type:<\/p>\n<pre id=\"bebf\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">val<\/strong> x = b.length        \/\/ compile error: b might be null<\/pre>\n<p id=\"c48f\" class=\"graf graf--p graf-after--pre\">And while this might seem cumbersome, it\u2019s really a breeze thanks to a few of its features. We still have smart casts, which casts nullable types to non-null wherever possible:<\/p>\n<pre id=\"a891\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">if<\/strong> (b == <strong class=\"markup--strong markup--pre-strong\">null<\/strong>) <strong class=\"markup--strong markup--pre-strong\">return<\/strong>\n<strong class=\"markup--strong markup--pre-strong\">val <\/strong>x = b.length        \/\/ no problem<\/pre>\n<p id=\"f317\" class=\"graf graf--p graf-after--pre\">We could also use a safe call\u00a0<code class=\"\" data-line=\"\">?.<\/code>, which evaluates to null instead of throwing a NPE:<\/p>\n<pre id=\"c58c\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">val <\/strong>x = b?.length       \/\/ type of x is nullable Int<\/pre>\n<p id=\"7c08\" class=\"graf graf--p graf-after--pre\">Safe calls can be chained together to avoid those nested if-not-null checks we sometimes write in other languages, and if we want a default value other than <code class=\"\" data-line=\"\">null<\/code> we can use the elvis operator\u00a0<code class=\"\" data-line=\"\">?:<\/code>:<\/p>\n<pre id=\"b5d1\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">val <\/strong>name = ship?.captain?.name ?: \"unknown\"<\/pre>\n<p id=\"053c\" class=\"graf graf--p graf-after--pre\">If none of that works for you, and you absolutely need a NPE, you will have to ask for it explicitly:<\/p>\n<pre id=\"d0e2\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">val <\/strong>x = b?.length ?: <strong class=\"markup--strong markup--pre-strong\">throw<\/strong> NullPointerException()  \/\/ same as below\n<strong class=\"markup--strong markup--pre-strong\">val <\/strong>x = b!!.length                                 \/\/ same as above<\/pre>\n<h4 id=\"49a6\" class=\"graf graf--h4 graf-after--pre\"><strong class=\"markup--strong markup--h4-strong\">16# <\/strong><a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/reference\/lambdas.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/reference\/lambdas.html\"><strong class=\"markup--strong markup--h4-strong\">Better\u00a0Lambdas<\/strong><\/a><\/h4>\n<p id=\"d4f9\" class=\"graf graf--p graf-after--h4\">Oh boy, is this a good lambda system\u200a\u2014\u200aperfectly balanced between readability and terseness, thanks to some clever design choices. The syntax is first of all straight forward:<\/p>\n<pre id=\"a181\" class=\"graf graf--pre graf-after--p\"><strong class=\"markup--strong markup--pre-strong\">val<\/strong> sum = { x: Int, y: Int -&gt; x + y }   \/\/ type: (Int, Int) -&gt; Int\n<strong class=\"markup--strong markup--pre-strong\">val <\/strong>res = sum(4,7)                      \/\/ res == 11<\/pre>\n<p id=\"014f\" class=\"graf graf--p graf-after--pre\">And here come the clever bits:<\/p>\n<ol class=\"postList\">\n<li id=\"ac3d\" class=\"graf graf--li graf-after--p\">Method parentheses can be moved or omitted if the lambda is the last or the only argument of a method.<\/li>\n<li id=\"9f63\" class=\"graf graf--li graf-after--li\">If we choose not to declare the argument of a single-argument-lambda it\u2019ll be implicitly declared under the name <code class=\"\" data-line=\"\">it<\/code>.<\/li>\n<\/ol>\n<p id=\"996a\" class=\"graf graf--p graf-after--li\">These facts combined makes the following three lines equivalent:<\/p>\n<pre id=\"50bc\" class=\"graf graf--pre graf-after--p\">numbers.filter({ x -&gt; x.isPrime() })\nnumbers.filter { x -&gt; x.isPrime() }\nnumbers.filter { it.isPrime() }<\/pre>\n<p id=\"3e41\" class=\"graf graf--p graf-after--pre\">And this allows us to write concise functional code\u200a\u2014\u200ajust look at this beauty:<\/p>\n<pre id=\"5501\" class=\"graf graf--pre graf-after--p\">persons\n    .filter { it.age &gt;= 18 }\n    .sortedBy { it.name }\n    .map { it.email }\n    .forEach { print(it) }<\/pre>\n<p id=\"b02d\" class=\"graf graf--p graf-after--pre\">Kotlin\u2019s lambda system combined with extension functions makes it ideal for <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/en.wikipedia.org\/wiki\/Domain-specific_language\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/en.wikipedia.org\/wiki\/Domain-specific_language\">DSL<\/a> creation. Check out <a class=\"markup--anchor markup--p-anchor\" href=\"https:\/\/github.com\/Kotlin\/anko\" target=\"_blank\" rel=\"noopener nofollow\" data-href=\"https:\/\/github.com\/Kotlin\/anko\">Anko<\/a> for an example of a DSL that aims to enhance Android development:<\/p>\n<pre id=\"1e7e\" class=\"graf graf--pre graf-after--p\">verticalLayout {\n    padding = dip(30)\n    editText {\n        hint = \u201cName\u201d\n        textSize = 24f\n    }\n    editText {\n        hint = \u201cPassword\u201d\n        textSize = 24f\n    }\n    button(\u201cLogin\u201d) {\n        textSize = 26f\n    }\n}<\/pre>\n<h4 id=\"1f64\" class=\"graf graf--h4 graf-after--pre\">17# <a class=\"markup--anchor markup--h4-anchor\" href=\"https:\/\/kotlinlang.org\/docs\/tutorials\/getting-started.html\" target=\"_blank\" rel=\"nofollow noopener\" data-href=\"https:\/\/kotlinlang.org\/docs\/tutorials\/getting-started.html\">IDE\u00a0Support<\/a><\/h4>\n<p id=\"b89d\" class=\"graf graf--p graf-after--h4\">You have a number of options if you intend to get started with Kotlin, but I highly recommend using <strong class=\"markup--strong markup--p-strong\">IntelliJ <\/strong>which <strong class=\"markup--strong markup--p-strong\">comes bundled with Kotlin<\/strong>\u2014its features demonstrate the advantage of having the same people design both language and IDE.<\/p>\n<p id=\"37fc\" class=\"graf graf--p graf-after--p\">Just to give you a minor but clever example, this thing popped up when I first tried to copy-paste some Java code from Stack Overflow:<\/p>\n<figure id=\"7f18\" class=\"graf graf--figure graf-after--p graf--trailing\">\n<div class=\"aspectRatioPlaceholder is-locked\"><\/div>\n<\/figure>\n<section class=\"section section--body section--first\">\n<div class=\"section-content\">\n<div class=\"section-inner sectionLayout--insetColumn\">\n<figure id=\"7f18\" class=\"graf graf--figure graf-after--p graf--trailing\">\n<div class=\"aspectRatioPlaceholder is-locked\">\n<div class=\"progressiveMedia js-progressiveMedia graf-image is-canvasLoaded is-imageLoaded\" data-image-id=\"1*zpE0UpuBDGW7Mk-Vtx2uKw.png\" data-width=\"962\" data-height=\"354\" data-action=\"zoom\" data-action-value=\"1*zpE0UpuBDGW7Mk-Vtx2uKw.png\" data-scroll=\"native\"><img data-recalc-dims=\"1\" decoding=\"async\" class=\"progressiveMedia-image js-progressiveMedia-image\" src=\"https:\/\/i0.wp.com\/cdn-images-1.medium.com\/max\/1043\/1%2AzpE0UpuBDGW7Mk-Vtx2uKw.png?w=910&#038;ssl=1\" data-src=\"https:\/\/cdn-images-1.medium.com\/max\/1043\/1*zpE0UpuBDGW7Mk-Vtx2uKw.png\" \/><\/div>\n<\/div><figcaption class=\"imageCaption\">IntelliJ will notice if you paste Java code into a Kotlin\u00a0file<\/figcaption><\/figure>\n<\/div>\n<\/div>\n<\/section>\n<section class=\"section section--body section--last\">\n<div class=\"section-divider\">\n<hr class=\"section-divider\" \/>\n<\/div>\n<\/section>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p class=\"excerpt\">It\u2019s time to start using a modern programming language: Eiffel In fact as I read Why you should totally switch to Kotlin \u2013 Magnus Vinther \u2013 Medium I wonderded why they haven&#8217;t switched to Eiffel, as it has had for decades most of the features they praised in Kotlin!<\/p>\n<p class=\"more-link-p\"><a class=\"more-link\" href=\"https:\/\/monodes.com\/predaelli\/2018\/04\/01\/its-time-to-start-using-a-modern-programming-language-eiffel\/\">Read more &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":4,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[34,98],"tags":[],"class_list":["post-3969","post","type-post","status-publish","format-standard","hentry","category-eiffel","category-liberty-eiffel"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p6daft-121","jetpack-related-posts":[{"id":9707,"url":"https:\/\/monodes.com\/predaelli\/2022\/10\/09\/eiffeldoc-any\/","url_meta":{"origin":3969,"position":0},"title":"Eiffeldoc: ANY","author":"Paolo Redaelli","date":"2022-10-09","format":false,"excerpt":"twin: ANY-- Return a new object with the dynamic type of Current. copy (other: ANY) -- Update current object using fields of object attached to other, so as to yield equal objects. Too bad that those Eiffel features, (function members) available in each and every object and variable are not\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9687,"url":"https:\/\/monodes.com\/predaelli\/2022\/10\/01\/spacevim-has-eiffel-support\/","url_meta":{"origin":3969,"position":1},"title":"SpaceVim has Eiffel support!","author":"Paolo Redaelli","date":"2022-10-01","format":false,"excerpt":"SpaceVim, a community-driven vim distribution that seeks to provide layer feature, besides turning Vim into a nifty IDE for several languages (C\/C++, Rust, Kotlin, Go, Python, Java and JavaScript plus others), it offers among the available layers one for Eiffel! \u00a0","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2137,"url":"https:\/\/monodes.com\/predaelli\/2017\/02\/02\/kotlin-programming-language\/","url_meta":{"origin":3969,"position":2},"title":"Kotlin Programming Language","author":"Paolo Redaelli","date":"2017-02-02","format":false,"excerpt":"Kotlin Programming Language Statically typed programming language for the JVM, Android and the browser 100% interoperable with Java\u2122 In bold the little difference between a successful language and a niche one.","rel":"","context":"In &quot;Senza categoria&quot;","block_context":{"text":"Senza categoria","link":"https:\/\/monodes.com\/predaelli\/category\/senza-categoria\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":13499,"url":"https:\/\/monodes.com\/predaelli\/2025\/06\/02\/design-by-contract-dbc-v-test-driven-design-tdd-wolands-cat\/","url_meta":{"origin":3969,"position":3},"title":"Design-by-Contract (DbC) v Test-Driven Design (TDD) | Woland&#8217;s cat","author":"Paolo Redaelli","date":"2025-06-02","format":false,"excerpt":"Design-by-Contract (DbC) v Test-Driven Design (TDD) from\u00a0 Woland's cat shortly: DbC = contracts + tests Design-by-Contract (DbC) v Test-Driven Design\u00a0(TDD) Posted on 04\/03\/2021 by wolandscat A software contract in the Eiffel language Another bit of software engineering knowledge from my archive relates to two well-known formal quality methods used in\u2026","rel":"","context":"In &quot;Eiffel&quot;","block_context":{"text":"Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2025\/06\/eiffel_contract.png?resize=350%2C200&ssl=1","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2025\/06\/eiffel_contract.png?resize=350%2C200&ssl=1 1x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2025\/06\/eiffel_contract.png?resize=525%2C300&ssl=1 1.5x, https:\/\/i0.wp.com\/monodes.com\/predaelli\/wp-content\/uploads\/sites\/4\/2025\/06\/eiffel_contract.png?resize=700%2C400&ssl=1 2x"},"classes":[]},{"id":8848,"url":"https:\/\/monodes.com\/predaelli\/2021\/10\/18\/not-deprecated\/","url_meta":{"origin":3969,"position":4},"title":"Not deprecated","author":"Paolo Redaelli","date":"2021-10-18","format":false,"excerpt":"\u00abIn Kotlin, loops are deprecated.\u00bb\u00a0 That's the title picked by Luc-Antoine Girardin. Of course they are not, in fact he starts the article with Well\u2026 That\u2019s not entirely true. It would not make sense to actually deprecate them as loops have been a part of programming for decades\u2026 They reality\u2026","rel":"","context":"In &quot;Tricks&quot;","block_context":{"text":"Tricks","link":"https:\/\/monodes.com\/predaelli\/category\/documentations\/tricks\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":9332,"url":"https:\/\/monodes.com\/predaelli\/2022\/05\/01\/liberty-liberty-eiffel-programming-language\/","url_meta":{"origin":3969,"position":5},"title":"Liberty | Liberty Eiffel programming language","author":"Paolo Redaelli","date":"2022-05-01","format":false,"excerpt":"Liberty Source: Liberty | Liberty Eiffel programming language","rel":"","context":"In &quot;Liberty Eiffel&quot;","block_context":{"text":"Liberty Eiffel","link":"https:\/\/monodes.com\/predaelli\/category\/eiffel\/liberty-eiffel\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/3969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/comments?post=3969"}],"version-history":[{"count":0,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/posts\/3969\/revisions"}],"wp:attachment":[{"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/media?parent=3969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/categories?post=3969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/monodes.com\/predaelli\/wp-json\/wp\/v2\/tags?post=3969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}