<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Pavel Patz on Medium]]></title>
        <description><![CDATA[Stories by Pavel Patz on Medium]]></description>
        <link>https://medium.com/@pavelpatz?source=rss-47d2bf422f0a------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*1_LMWsvGq_cjtM2C.jpg</url>
            <title>Stories by Pavel Patz on Medium</title>
            <link>https://medium.com/@pavelpatz?source=rss-47d2bf422f0a------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Fri, 04 Dec 2026 21:38:46 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@pavelpatz/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Using LDAC (and aptX) codecs for Bluetooth devices on openSuse Tumbleweed (and possibly other…]]></title>
            <link>https://medium.com/@pavelpatz/using-ldac-and-aptx-codecs-for-bluetooth-devices-on-opensuse-tumbleweed-and-possibly-other-7e65e73c436?source=rss-47d2bf422f0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/7e65e73c436</guid>
            <category><![CDATA[sound]]></category>
            <category><![CDATA[linux]]></category>
            <category><![CDATA[pulseaudio]]></category>
            <category><![CDATA[bluetooth]]></category>
            <category><![CDATA[ldac]]></category>
            <dc:creator><![CDATA[Pavel Patz]]></dc:creator>
            <pubDate>Sun, 09 Feb 2020 20:45:09 GMT</pubDate>
            <atom:updated>2020-02-09T20:57:22.129Z</atom:updated>
            <content:encoded><![CDATA[<h3>Using LDAC (and aptX) codecs for Bluetooth devices on openSuse Tumbleweed (and possibly other Linux distros)</h3><p>LDAC is so far the <a href="https://www.audioholics.com/audio-technologies/bluetooth-audio-guide/page-2">most superior codec</a>, developed by SONY, and used for audio transmission over Bluetooth. It’s capable of 24bit @ 96 kHz (thus supports MQA or HiRes FLAC formats) and throughput up to 990 kbps (depending on the device capabilities and the distance and physical barriers between receiver and transmitter). The “only” drawback could be that you can find it mostly only on Sony devices.</p><p>aptX and especially aptX HD then provides less “quality” audio (good enough for services like Spotify though), but still far more better than SBC (sub-band codec) which is the only one required by Bluetooth standard, but also the most essential one, providing the worst quality (except the HFP/HSP, of course :)). aptX’s advantage is then that it’s supported by much wider range of different devices.</p><p>While developed as typical proprietary codec, it was open-sourced not-so-long time ago, so it could be <a href="https://www.theverge.com/2017/3/21/15004562/sony-google-android-o-ldac-wireless-music">included in Android Open Source Project</a> (sorry, iPhone users). And then, there was just a small step needed to be done, to support this codec also on other Linux platforms. This is managed by project <a href="https://github.com/EHfive/pulseaudio-modules-bt">pulseaudio-modules-bt</a> which forks original PulseAudio’s Bluetooth Module and adds support for more higher-quality codecs (LDAC, aptX, aptX HD and AAC).</p><p>The common problem with regular Linux distributions is that they are using the latter one mentioned by default— which contains only mandatory SBC. It could be because, except LDAC, all other added codecs in the first one are simply proprietary and under some kind of licencing.</p><h3>Let’s do it</h3><p>So, enough talking and start improving our listening experience. These steps are focused on openSuse Tumbleweed (package names, etc.) but can be slightly modified and applied on other distributions as well.</p><p>At first we may want to check what codecs are supported by our device. To do this, just run shell command (it’s available in bluez-test package which might be needed to be installed first as it isn’t installed by default on our TW):</p><pre>avinfo &lt;bluetooth device address&gt;</pre><p>Please note, that your device needs to be on and disconnected before running it. The output would look like this:</p><pre>cdome@andromeda:~ $ avinfo AA:BB:CC:DD:EE:FF<br>Connecting ... <br>Stream End-Point #1: Audio Sink <br>        <strong>Media Codec: SBC</strong><br>                Channel Modes: Mono DualChannel Stereo JointStereo<br>                Frequencies: 16Khz 32Khz 44.1Khz 48Khz <br>                Subbands: 4 8<br>                Blocks: 4 8 12 16 <br>                Bitpool Range: 2-53<br>        Content Protection: SCMS-T<br>Stream End-Point #2: Audio Sink <br>        <strong>Media Codec: Vendor Specific A2DP Codec</strong><br>                Vendor ID 0x0000012d<br>                Vendor Specific Codec ID 0x00aa<br>                Vendor Specific Data: 0x3c 0x07<br>                Vendor Specific Value (LDAC)<br>                        Frequencies: 44.1kHz 48kHz 88.2kHz 96kHz <br>                        Channel modes: Mono Dual Stereo <br>        Content Protection: SCMS-T<br>Stream End-Point #3: Audio Sink <br>        <strong>Media Codec: MPEG24</strong><br>                Object Types: MPEG-2 AAC LC MPEG-4 AAC LC <br>                Frequencies: 8kHz 11.025kHz 12kHz 16kHz 22.05kHz 24kHz 32kHz 44.1kHz 48kHz <br>                Channels: 1 2 <br>                Bitrate: 320000<br>                VBR: Yes<br>        Content Protection: SCMS-T</pre><p>You can see, that our device supports three codecs — obligatory SBC, MPEG24 which means AAC and <em>Vendor Specific</em> one — which is LDAC in this case. In the table below you can find a list of some common codecs and their Vendor Specific IDs:</p><pre>Mandatory:<br>0x00 - SBC</pre><pre>Optional:<br>0x01 - MPEG-1,2 (aka MP3)<br>0x02 - MPEG-2,4 (aka AAC)<br>0x04 - ATRAC</pre><pre>Vendor specific:<br>0xFF 0x004F 0x01   - aptX<br>0xFF 0x00D7 0x24   - aptX HD<br>0xFF 0x000A 0x02   - aptX Low Latency<br>0xFF 0x00D7 0x02   - aptX Low Latency<br>0xFF 0x000A 0x01   - FastStream<br>0xFF 0x012D 0xAA   - LDAC<br>0xFF 0x0075 0x0102 - Samsung HD<br>0xFF 0x0075 0x0103 - Samsung Scalable Codec<br>0xFF 0x053A 0x484C - Savitech LHDC</pre><pre>0xFF 0x000A 0x0104 - The CSR True Wireless Stereo v3 Codec ID for AAC<br>0xFF 0x000A 0x0105 - The CSR True Wireless Stereo v3 Codec ID for MP3<br>0xFF 0x000A 0x0106 - The CSR True Wireless Stereo v3 Codec ID for aptX</pre><p>Since our device support some of the better codecs (and even LDAC), we need to install support for them — just look for our desired <a href="https://software.opensuse.org/package/pulseaudio-modules-bt?search_term=pulseaudio-modules-bt">pulseaudio-modules-bt in package search</a>, choose one of those community repos and install it — it would replace originally installed pulseaudio-module-bluetooth package.</p><p>Now you should restart the pulseaudio system by running</p><pre>pulseaudio -k</pre><p>as regular user (no need of machine restart). This would kill current PulseAudio instance and spawn new one. Now you can connect your BT device and check that it’s using better quality codec:</p><pre>pactl list | grep a2dp_codec</pre><p>You should see something like this:</p><pre>cdome@andromeda:~ $ pactl list | grep a2dp_codec<br>                <strong>bluetooth.a2dp_codec = &quot;LDAC&quot;</strong></pre><p>And you are done. Now you can enjoy your excessively expensive device with the best sound quality available.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7e65e73c436" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Going Kotlin: Judgement Day]]></title>
            <link>https://medium.com/@pavelpatz/going-kotlin-judgement-day-c64812690b0d?source=rss-47d2bf422f0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/c64812690b0d</guid>
            <category><![CDATA[functional-programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[jvm]]></category>
            <category><![CDATA[kotlin]]></category>
            <dc:creator><![CDATA[Pavel Patz]]></dc:creator>
            <pubDate>Sat, 26 Oct 2019 20:35:09 GMT</pubDate>
            <atom:updated>2019-11-05T05:20:36.944Z</atom:updated>
            <content:encoded><![CDATA[<p>In the first <a href="https://medium.com/@pavelpatz/going-kotlin-first-blood-a1960128abf0">post,</a> we have just started talking about all those features that Kotlin may bring a light into your regular, shady day of a Java developer.</p><p>Let’s have some more fun and let’s talk about its functional features.</p><p>At first, and it has to be said, that there is one and only one Functional Language — and it’s <a href="https://www.haskell.org/">Haskell</a> :) All others are just hybrids, more or less following FP and other paradigms. So if we would say that Kotlin is fully functional language — it’s not — and I’m sorry I mystified you in previous <a href="https://medium.com/@pavelpatz/going-kotlin-first-blood-a1960128abf0">post</a>. Let’s call it Marketing... Kotlin is just a language that takes a lot of inspiration from this world, but won’t and cannot be “just functional language”.</p><p>So, let’s start over.</p><h3>Lambdas</h3><p>From all you’ve may heard, one of the crucial parts of Functional Programming are “Lambdas”. And — you are right. Every other so-called-FP language has to have this Greek letter</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/110/1*Y0XDkxmbwa8cVdRdBDmTQg.png" /><figcaption>Greek letter Lambda</figcaption></figure><p>in its logo. So we have modern Clojure</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/400/1*FL-SKdLmCTKDwoasaJdaOA.png" /><figcaption>Clojure logo</figcaption></figure><p>Or our Good Ol’ Haskell</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*DPprIxydDTVVbmxMn6a5Cg.png" /><figcaption>Haskel Logo</figcaption></figure><p>We may have some conscious languages, that don’t use it, like LISP with</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/300/1*jYDxyd6xELBkXCCcckXTQw.png" /></figure><p>But Lambda letter is far more popular</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*52hXEubzRqStiNJhgOhHBg.png" /><figcaption>HalfLIfe</figcaption></figure><p>Lambda calculus is something that shaped our perception of computing. It was one of those things that put together math and computer science. It was one of those things that made math applicable on machines. Lambda Calculus is Applied Math.</p><p>But that doesn’t mean that we cannot have a great source of fun here, as most all of its current fans (lambda calculus is Turing-complete) are fighting with other paradigms (that are, of course, also Turing-complete) because of their expressibility.</p><h3>So. What are Lambdas about? Really!</h3><p>That was just a “funny” introduction. Better, let’s get back to the topic. Lambdas are functions without a name. Inline functions if you want to. Let’s have this meta-definition of function (it’s not written in Kotlin yet, it’s just some idea now, how the function should look like):</p><pre>doIt(int, lambdaFunction) -&gt; Int</pre><p>in this case lambdaFunction is a lambda function — anonymous function that is passed as a argument of doIt function call. Now it depends on doIt function implementation how it would use lambdaFunction function.</p><p>Let’s say that we would use our lambdaFunction to get run some math on its argument and doIt function would just validate the input.</p><p>So — let’s give it a real shape. We have this class:</p><pre><strong>class </strong>MyFunctions {<br>    <strong>fun </strong>double(number: Int) = number * 2<br>    <strong>fun </strong>triple(number: Int) = number * 3<br>}</pre><p>There are two functions — first one is returning double of its parameter, second one is returning a triple. Easy.</p><p>And let’s have our doIt function:</p><pre><strong>fun </strong>doIt(int: Int, lambdaFunction: (Int) -&gt; Int) =<br>    <strong>if </strong>(int &gt; 0)  lambdaFunction(int) <strong>else throw<br>        </strong><em>IllegalArgumentException</em>(&quot;Number is not positive enough.&quot;)</pre><p>Now we have a function that takes other function as a parameter, validates its input and then returns the result of arrived function, applied on given parameter. We can also see that our lambdaFunction parameter is a function that takes one integer as an input and returns integer. So, let’s use it:</p><pre><strong>val </strong>mf = <em>MyFunctions</em>()<br><em>println</em>(<em>doIt</em>(5, mf::double))<br><em>println</em>(<em>doIt</em>(5, mf::triple))<br><em>println</em>(<em>doIt</em>(-10, mf::double))</pre><p>Now, we are using instance of our MyFunction class and passing its functions as parameters into doIt method.</p><p>But — as we said — lambdas are anonymous and/or inline functions, so what keeps us from doing this</p><pre><em>println</em>(<em>doIt</em>(15, <strong>{</strong>number<em> -&gt; </em>number*4<strong>}</strong>))</pre><p>We have just put our function as a parameter and now it’s really anonymous and inline.</p><p>We can, of course simplify this as such function with one parameter has default parameter naming — and it’s it:</p><pre><em>println</em>(<em>doIt</em>(15, <strong>{ </strong>it*4<strong>}</strong>))</pre><p>And — in Kotlin we can put it’s lambda outside of parenthesis — so if we won’t be immediately printing the result value, but just reusing it, it would look like this:</p><pre><strong>val </strong>result = <em>doIt</em>(15) <strong>{ it </strong>* 4 <strong>}</strong></pre><p>That’s cool — because we are now using a block of code that is controlled by the method call in front of it. And if it’s just a block of code, we can do more:</p><pre><strong>val </strong>result = <em>doIt</em>(15) <strong>{<br>  val </strong>someOtherValue = someOtherValueFunction()<br>  if (someOtherValue &gt; 0) then it * someOtherValue else it * 4<br>}</pre><p>And the result is just the result of the method executing this block of code.</p><p>Do you remember when we were talking about that if or when are just an expressions? Now you have the idea.</p><h3>We are just lazy</h3><p>What is most important, is that those functions are evaluated only when they are used by their users. Let’s have some function like this:</p><pre>fun reallyComplexComputation(): String {<br>  // do some really really complex stuff here<br>  return resultOfSomeReallyComplexComputation<br>}</pre><p>and try to put its result into another function as a parameter:</p><pre>someOtherFunction(reallyComplexComputation())</pre><pre>fun someOtherFunction(str:String) {<br>  if (conditionsAreMet) {<br>    useResult(str)<br>  } else {<br>    doSometingElse()<br>  }<br>}</pre><p>Now you are trapped. You have to evaluate a result of reallyComplexComputation function always — no matter if you would use its output or not.</p><p>But — when you would use this function alone as a parameter of someOtherFunction, instead of its result:</p><pre>fun someOtherFunction(lambda: () -&gt; String) {<br>  if (conditionsAreMet) {<br>    useResult(lambda())<br>  } else {<br>    soSomethingElse()<br> }<br>}</pre><p>you may then call it this way</p><pre>someOtherFunction { reallyComplexComputation() }</pre><p>and reallyComplexComputation would be executed and evaluated only when needed — when actually called from success if statement branch. It’s called <strong>lazy evaluation</strong>. And we already used it — do you remember our doIt function, validating the input? Now it makes double sense — we can pass any function or block of code to our method and also make it executed only when it has to be executed and evaluated.</p><h3>Lambdas are everywhere</h3><p>This feature is used by many libraries built on Kotlin and there’s lot of Kotlin API using this approach.</p><p>For instance — you want to use some logger. If you were using Java, you may remember this:</p><pre>if (log.isInfoEnabled()) {<br>  log.info(&quot;We have done this &quot; + input.getSomeString())<br>}</pre><p>Which is nice because you are calling getSomeString() method only when needed, but it takes two more lines — and whole if here just to log a message. It makes your code more complex, less readable, longer.</p><p>You can, of course, just call it like this:</p><pre>log.info(&quot;We have done this &quot; + input.getSomeString())</pre><p>which is shorter and simpler, but getSomeString() would be evaluated every time, no matter what your log level is.</p><p>But — wait — this a job for lambdas, right? We could just write</p><pre>log.info { &quot;We have done this &quot; + input.getSomeString() }</pre><p>and pass our message as a lambda — now it would depend on our logging framework if it decides that our log level is high enough to print this message and thus evaluate it.</p><p>And you don’t need to write it by yourself — there is already such <a href="https://github.com/MicroUtils/kotlin-logging">library</a>.</p><p>Kotlin API brings several similar features like this. Is this pattern familiar to you?</p><pre>public String foo(String bar1, Integer bar2) {<br>  if (bar1 == null || bar1.equals(&quot;&quot;)) {<br>   throw new IllegalArgumentException(&quot;bar1 as to have a value&quot;);<br>  }<br>  if (bar2 == null || bar2 &lt; 0) {<br>    throw new IllegalArgumentException(&quot;bar2 has to be non-negative number&quot;)<br>  // Finally, do SOMETHING<br>}</pre><p>In Kotlin, there are prepared lambda-based functions for this:</p><pre>fun foo(bar1: String?, bar2:Int?) {<br>  check(bar1 != null &amp;&amp; bar1.equals(&quot;&quot;)) { &quot;bar1 as to have a value&quot; }</pre><pre>  check(bar2 != null &amp;&amp; bar2 &gt;= 0) { &quot;bar2 has to be non-negative number&quot; }</pre><p>And because we are using Kotlin — of course, we can say that the input cannot be null at all:</p><pre>fun foo(bar1: String, bar2:Int) {<br>  check(bar1 != &quot;&quot;) { &quot;bar1 as to have a value&quot; }</pre><pre>check(bar2 &gt;= 0) { &quot;bar2 has to be non-negative number&quot; }</pre><p>Isn’t that nice?</p><h3>Standard Functions</h3><p>There are other nice concepts of FP in Kotlin — like higher-order functions. And we will get back to them in the future.</p><p>But what is, perhaps, the best on this language is its set of <strong>standard functions</strong>. Functions that are available on every object, basically taking just one parameter — and that one is lambda function.</p><p>As this is so nice and cool enough feature so we will get to it in the separate post.</p><h3>Stay Tuned</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=c64812690b0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Going Kotlin: First Blood]]></title>
            <link>https://medium.com/@pavelpatz/going-kotlin-first-blood-a1960128abf0?source=rss-47d2bf422f0a------2</link>
            <guid isPermaLink="false">https://medium.com/p/a1960128abf0</guid>
            <category><![CDATA[kotlin]]></category>
            <category><![CDATA[programming]]></category>
            <category><![CDATA[java]]></category>
            <category><![CDATA[introduction]]></category>
            <dc:creator><![CDATA[Pavel Patz]]></dc:creator>
            <pubDate>Thu, 24 Oct 2019 07:03:37 GMT</pubDate>
            <atom:updated>2020-03-07T06:38:09.096Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fqN-Uc1rOKlDwckKSI_xBw.png" /><figcaption>That’s true Kotlin. Just an island in Finskiy Zaliv.</figcaption></figure><p>Check also Part 2: <a href="https://medium.com/@pavelpatz/going-kotlin-judgement-day-c64812690b0d">Going Kotlin — Judgement Day</a></p><p>Once upon a time, I’ve got an option to leave our old, very <em>old product</em> I was working on, and start something new. <em>Old product</em> is a mixture of ancient coding style, in-house frameworks (yep, there was so so Hibernate in 2002) and others of creative C-C++-Java ideas.</p><p>So I was eager for this option — we are going to work on a new thing, we are GOING TO THE CLOUD with our new service.</p><p>Our proposed service was not the first, but, it was not also the hundredths in the row. And — all in all — WE are going to choose all technologies, that WE need for OUR service to run in OUR cloud solution.</p><p>So — after going for years with Java 8 — I didn’t look back and took Kotlin. I may perhaps be missing lot of nice new Java 11 features, but I don’t think I am starving.</p><p>I was using Kotlin for years before, for my hobby Android projects, but now — this just become OFFICIAL.</p><p>For lot of people — it may not have been an “established” language — but… It has lot of its advantages and signs that this language is being more and more taken seriously.</p><p>Let’s start with the other thing — Kotlin is not just another language in the row, it’s really an option:</p><ul><li>It’s preferred language for <a href="https://developer.android.com/kotlin/">Android development</a> (yep, there may be some political reasons, but it doesn’t change a thing)</li><li>It’s taken seriously by <a href="https://gradle.org/kotlin/">Gradle</a> build tool</li><li>Old-time players like Spring are <a href="https://spring.io/blog/2017/01/04/introducing-kotlin-support-in-spring-framework-5-0">supporting Kotlin</a></li><li>And there are new game-changing frameworks like <a href="https://micronaut.io/">Micronaut</a> which <a href="https://guides.micronaut.io/creating-your-first-micronaut-app-kotlin/guide/index.html">takes Kotlin</a> as supported from the beginning</li><li>Several library creators are adding their <a href="https://github.com/FasterXML/jackson-module-kotlin">full support</a> or even extensions for this language. And there is even more of 3rd party <a href="https://litote.org/kmongo/">extensions</a> to <a href="https://github.com/debop/koda-time">libraries</a> you know.</li></ul><h3>What makes this language so cool?</h3><p>The answer may be “what makes this language so common” — it’s its 1:1 compatibility with Java — yet bringing tons of new features. It’s not like Groovy, which is really cool, but heavily depends on reflection, it’s more like Scala which compiles directly to reasonable bytecode, just better and more supported.</p><p>Kotlin brings in</p><ul><li><strong>non-null</strong> properties by default</li><li><strong>immutability</strong> by simple definition</li><li><strong>just the code</strong>, “boilerplate code reduced by 99%”(TM)</li><li>higher-level functions and full <strong>functional-way</strong> of programming in its API</li><li>lots of other features</li></ul><p>Let’s go step by step (TL;DR: The most interesting things would be covered of course in following post):</p><h3>Non-nullable properties</h3><p>In Kotlin — the default approach means that every property, every field, every variable has to have a value. Null value is simply really unexpected and you are forced by the language to avoid it or deal with it.</p><p>If you would have method definition like</p><pre>fun foo(bar: String)</pre><p>bar cannot ever be null so you can drop all your extra checks like</p><pre>if(bar != null &amp;&amp; bar.equals(&quot;awesome&quot;))</pre><p>and — in this case — you can even just write</p><pre>if(bar == &quot;awesome&quot;)</pre><p>What a miracle! Double-equals operator even means calling equals(). There are some cases where it’s more annoying like — comparing different number types, but it’s working really nice otherwise even with complex data types and collections.</p><p>Of course, if you would like to use nullable variables — you can, and then you have plenty of operators that would help you working with them.</p><pre>myNullableString?.substring(1)</pre><p>would just return you a new substring if myNullableString has a value or null when there’s nothing to be “substringed”</p><p>You can use Elvis operator (?: — do you recognize the hairstyle there?)</p><pre>myNullableString?.substring(1)?:&quot;&quot;</pre><p>which would you return you an empty string, or even</p><pre>myNullableString?.substring(1)?:throw InvalidInputException(&quot;WTF&quot;)</pre><p>when you need to tell the world your feelings about having null reference.</p><p>And you can chain these:</p><pre>myNullableString?.substring(1)?.toUpperCase?.split(&quot;A&quot;)?:throw InvalidInputException(&quot;WTF&quot;)</pre><p>Btw — do you remember these?</p><pre>if (parameter != null &amp;&amp; parameter.getConfig() != null &amp;&amp;            parameter.getConfig().getValidity() != null &amp;&amp; parameter.getConfig.getValidity().getTo() != null) {<br>  return parameter.getConfig().getValidity().getTo().format();<br>} else {<br>  return &quot;N/A&quot;<br>}</pre><p>This looks a bit better, doesn’t it?</p><pre>parameter?.config?.validity?.to?.format()?:&quot;N/A&quot;</pre><p>And if you are sure that, in this particular case, yourNullableStringcannot be null? And if it would be, it’s just an error? Let’s shout it!!</p><pre>yourNullableString!!.substring(1)</pre><p>This would cause NPE if given variable would be null. But if it is not, you can then use this property without checking it again and again and again</p><pre>yourNullableString!!.substring(1)<br>yourNullableString.toLowerCase()</pre><h3>Immutability is not new</h3><p>It’s new, at least in this case — anyway — you could always say that something is final. In Kotlin — if you say it’s val, it is final.</p><pre>val foo = &quot;bar&quot;</pre><pre>foo = &quot;foo&quot; // won’t work. either compile or run time.</pre><p>And you are supposed to use val as much as possible. You need to change the object property value? Yes, you may use var instead— to say that your variable can be changed. Or — you can just make a copy and change just a single field during the copying. Creating of new objects is cheap in JVM, didn’t they tell you?</p><pre>myCoolObject.copy(myPropertyToByChanged = betterValue)</pre><p>Did you notice, that you can use parameter naming in method calls? That’s another cool thing.</p><h3>Just the code</h3><p>Kotlin can be brief as your briefs. Sorry for this joke, but — like, using <em>Lombok</em>, you just define fields of POJO and that’s it. Just better — no extra dependencies, no extra annotations. No need of getters and setters and equals and hashcodes and toStrings … Just fields. And some more benefits like method .copy() mentioned above. Wow. Like an other any modern language. This is just proper and compatible POJO class written in Kotlin:</p><pre><strong>data</strong> class MyAwesomeClass(val awesomeProperty: String, val lessAwesomeNumber: Int)</pre><p>That’s it — now you have a class with two properties with all those setters and getters, etc, which you can use even from Java. I’m sorry I won’t post a Java version of this class but I don’t want to make this post longer more than needed. And as a bonus — this class is immutable. Vals — do you see them? And final — so by default, you cannot inherit from it.</p><p>Just don’t forget the data keyword there — without it, it would be just regular, dumb class as you know it.</p><h3>Functional language</h3><p>Kotlin is also a fully <strong>functional language</strong> — it not only supports functions as first-class citizens but it also easily supports all those lambdas and functions of higher order. Not only by design, but also in its API.</p><p>And we will cover this topic in another post as there’s really lot to cover.</p><p>But… For now — just some preview:</p><p>You may say, that everything should be an expression. You are right.</p><pre>val x = if(xx) yy else zz</pre><p>is valid as if is an expression. This also kicked the famous ternary ?: operator out of this language.</p><pre>val y = throw ItsSoUglyException()</pre><p>is an expression too (even the assignment is unreachable).</p><pre>val z = try {<br> something.returnDangerousValue()<br>} catch (e: SomeException) {<br> defaultValue<br>}</pre><p>is and expression as well.</p><p>It makes sense, because these are just functions and their “blocks” just expressions.</p><h3>Lots of stuff elsewhere</h3><p>We all hate static stuff — and Kotlin is same. So, there are no statics in this language. Even when you want to have something <em>static</em> and do this</p><pre><strong>object</strong> MoreOrLessStaticClass {<br>  fun staticMethod() {<br>    //...<br>  }<br>}</pre><p>so you can call it like this</p><pre>MoreOrLessStaticClass.staticMethod()</pre><p>is not really static — it’s just a singleton in reality. Does it sound cool enough?</p><p>Do you remember switch statements from other languages? Well, Kotlin doesn’t have it. But don’t be sad, it has something better: when statement.</p><p>See this:</p><pre>when(myField) {<br>  SomeEnum.A -&gt; print(&quot;It&#39;s A&quot;)<br>  SomeEnum.B -&gt; print(&quot;It&#39;s B&quot;)<br>  SomeEnum.B -&gt; print(&quot;It&#39;s C&quot;)<br>}</pre><p>Ok. This is not so cool, it’s just switch. But — do you remember that everything is an expression?</p><pre>val message = when(myField) {<br>  SomeEnum.A -&gt; &quot;It&#39;s A&quot;<br>  SomeEnum.B -&gt; &quot;It&#39;s B&quot;<br>  SomeEnum.B -&gt; &quot;It&#39;s C&quot;<br>}</pre><p>Not a big difference, right? Well, it depends — yes we are returning a value now, but also — because we are returning a value, we <strong>have to cover all possibilities</strong> in our when branches. That means that our compiler would immediately inform us that SomeEnum has changed not only by removing some its value but also by adding new one.</p><p>And that’s not all what when can do:</p><pre>val result = when (someProperty) {<br>  is Int -&gt; &quot;It&#39;s Integer!&quot;<br>  &quot;a&quot;, &quot;b&quot; -&gt; &quot;It&#39;s String a or b&quot;<br>  in someCollection -&gt; &quot;We can find it in our collection&quot;<br>  else -&gt; throw IllegalArgumentException(&quot;Not sure what to do here&quot;)</pre><p>It’s a mix without a sense here… But it shows you more ways how to use switch. You can mix different test types, you can check for a type by is or its appearance in some collection by in. Or just enumerate particular constants to be compared. Or just throw an exception when you want to. Do you remember that</p><pre>val results = throw IllegalArgumentException(&quot;Not sure what to do&quot;)</pre><p>is an expression, even it doesn’t make sense? When used together in more complex assignments like this it makes complete syntactical sense.</p><p>And, there’s much more to do and to enjoy, just don’t want to make this post more longer than it can be readable. Stay tuned.</p><h3>Real compatibility</h3><p>After all of this — you may ask — where’s the 1:1 compatibility with Java? I have to write something really static. I have to override Kotlin way of thinking and do it in good-ol’ Java 1.1 style.</p><p>Well — you can — there’s a plenty of annotations that would allow you to bend the Kotlin compiler in the way you want — e.g. check this <a href="https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.jvm/-jvm-static/">one</a>. You can do plenty of officially-supported <em>hacks</em> to be really really compatible. But from the other side — Kotlin is also doing lot of <em>hacks</em> to be understandable better from Java. For instance — do you remember the moment when we were talking about non-nullability? Kotlin, at least, automatically annotates all such variables with @NotNull — it may not help your compiler, but it would help your IDE, your colleagues or your validators to better understand your API possibilities.</p><h3>To Be Continued…</h3><p>There’s a lot of stuff to love. Much more than I was able to cover here.</p><p>This was quite brief introduction to some of features of this language that make it awesome. If you would like, we can dive deeper into some topics in some of next posts, just let me know.</p><p>Anyway.</p><p>We have to talk about its FP features more. And also — after we started loving this language — do we want to see how it would work with the rest of the world? Definitely.</p><p>Wait for the following post, we have just started.</p><h3>Stay tuned.</h3><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a1960128abf0" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>