<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" ><generator uri="https://jekyllrb.com/" version="3.9.2">Jekyll</generator><link href="/blog/feed.xml" rel="self" type="application/atom+xml" /><link href="/blog/" rel="alternate" type="text/html" /><updated>2023-01-25T06:09:39+00:00</updated><id>/blog/feed.xml</id><title type="html">fun Kotlin.fact()</title><subtitle>Kotlin trivia, hints and productivity tips</subtitle><entry><title type="html">Fact #3: Markdown in documenting comments</title><link href="/blog/jekyll/update/2021/07/22/fact-3-markdown-in-documenting-comments.html" rel="alternate" type="text/html" title="Fact #3: Markdown in documenting comments" /><published>2021-07-22T12:23:12+00:00</published><updated>2021-07-22T12:23:12+00:00</updated><id>/blog/jekyll/update/2021/07/22/fact-3-markdown-in-documenting-comments</id><content type="html" xml:base="/blog/jekyll/update/2021/07/22/fact-3-markdown-in-documenting-comments.html">&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;: Where Java uses verbose HTML markup, in Kotlin you can use Markdown.&lt;/p&gt;

&lt;p&gt;Kotlin uses &lt;a href=&quot;https://kotlinlang.org/docs/kotlin-doc.html&quot;&gt;KDoc&lt;/a&gt; to document the code. It’s not an extension of Javadoc,
but not by accident it’s similar to it. However, Kotlin designers decided to go another way for certain features.&lt;/p&gt;

&lt;p&gt;Let’s take a look at an example Javadoc:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-java&quot; data-lang=&quot;java&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**
 * Here I'm going to list some &amp;lt;b&amp;gt;important things&amp;lt;/b&amp;gt; to know when calling this function:
 * &amp;lt;ul&amp;gt;
 * &amp;lt;li&amp;gt;first item&amp;lt;/li&amp;gt;
 * &amp;lt;li&amp;gt;second item - look at &amp;lt;a href=&quot;example.com&quot;&amp;gt;this link&amp;lt;/a&amp;gt;&amp;lt;/li&amp;gt;
 * &amp;lt;li&amp;gt;third item&amp;lt;/li&amp;gt;
 * &amp;lt;/ul&amp;gt;
 *
 * Paragraph 2
 * &amp;lt;br&amp;gt;
 * Paragraph 3
 */&lt;/span&gt;
&lt;span class=&quot;kd&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;kt&quot;&gt;void&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;someImportantFunction&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;{&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;HTML looks a big clunky these days, right? It certainly adds some noise - extra effort when both writing and reading it.
It wasn’t really designed to be readable as code, in contrast to Markdown.&lt;/p&gt;

&lt;p&gt;Converting the above to Kotlin gives us:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-kotlin&quot; data-lang=&quot;kotlin&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**
 * Here I'm going to list some **important things** to know when calling this function:
 *
 *  * first item
 *  * second item - look at [this link](example.com)
 *  * third item
 *
 * Paragraph 2
 *
 * Paragraph 3
 */&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;fun&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;someImportantFunction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;Looks simpler, right?&lt;/p&gt;

&lt;p&gt;You may say that some modern IDEs actually render the documenting comments. It’s true:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/blog/assets/rendered-documenting-comment.png&quot; alt=&quot;Rendered documenting comment&quot; /&gt;&lt;/p&gt;

&lt;p&gt;However, not all tools do it. For example, during code review, or simply browsing on GitHub or BitBucket. Editing the
comment in a WYSIWYG manner is also in theory possible, but since most of us already got used to Markdown, we can use it
as well and appreciate KDoc for this improvement.&lt;/p&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html">TL;DR: Where Java uses verbose HTML markup, in Kotlin you can use Markdown.</summary></entry><entry><title type="html">Fact #2: Easy migration from deprecated APIs</title><link href="/blog/jekyll/update/2021/05/17/fact-2-easy-migration-from-deprecated-apis.html" rel="alternate" type="text/html" title="Fact #2: Easy migration from deprecated APIs" /><published>2021-05-17T07:18:12+00:00</published><updated>2021-05-17T07:18:12+00:00</updated><id>/blog/jekyll/update/2021/05/17/fact-2-easy-migration-from-deprecated-apis</id><content type="html" xml:base="/blog/jekyll/update/2021/05/17/fact-2-easy-migration-from-deprecated-apis.html">&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;: Alt+Enter is your friend when moving away from deprecated APIs in Kotlin.&lt;/p&gt;

&lt;p&gt;Deprecations happen when library authors come up with a better way to handle a specific use case. It often saves library
maintainers’ time because of no further need to support the old API, but creates an extra overhead for library
consumers because they need to migrate their code.&lt;/p&gt;

&lt;p&gt;Kotlin has a nice little feature that facilitates this process. For example, the recent deprecations around Time API
resulted in such warning when switching to Kotlin 1.5.0:&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/blog/assets/deprecation-use-site.png&quot; alt=&quot;Deprecated Time API&quot; /&gt;&lt;/p&gt;

&lt;p&gt;The clue is that together with the deprecation notice, &lt;strong&gt;IntelliJ offers an action to replace with the new recommended
API&lt;/strong&gt;. This is possible thanks to the extra piece of info in the &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;@Deprecated&lt;/code&gt; annotation:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-kotlin&quot; data-lang=&quot;kotlin&quot;&gt;&lt;span class=&quot;nd&quot;&gt;@Deprecated&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
    &lt;span class=&quot;s&quot;&gt;&quot;Use lowercase() instead.&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;nc&quot;&gt;ReplaceWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;lowercase(Locale.getDefault())&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;java.util.Locale&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;where &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;ReplaceWith&lt;/code&gt; is defined like this:&lt;/p&gt;

&lt;figure class=&quot;highlight&quot;&gt;&lt;pre&gt;&lt;code class=&quot;language-kotlin&quot; data-lang=&quot;kotlin&quot;&gt;&lt;span class=&quot;cm&quot;&gt;/**
* Specifies a code fragment that can be used to replace a deprecated function, property or class. Tools such
* as IDEs can automatically apply the replacements specified through this annotation.
*
* @property expression the replacement expression. The replacement expression is interpreted in the context
*     of the symbol being used, and can reference members of enclosing classes etc.
*     For function calls, the replacement expression may contain argument names of the deprecated function,
*     which will be substituted with actual parameters used in the call being updated. The imports used in the file
*     containing the deprecated function or property are NOT accessible; if the replacement expression refers
*     on any of those imports, they need to be specified explicitly in the [imports] parameter.
* @property imports the qualified names that need to be imported in order for the references in the
*     replacement expression to be resolved correctly.
*/&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@Target&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@Retention&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nc&quot;&gt;BINARY&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;nd&quot;&gt;@MustBeDocumented&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;public&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;annotation&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;ReplaceWith&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;expression&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;vararg&lt;/span&gt; &lt;span class=&quot;kd&quot;&gt;val&lt;/span&gt; &lt;span class=&quot;py&quot;&gt;imports&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;String&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;

&lt;p&gt;It’s helpful for both sides of the library API: author and consumer. IntelliJ doesn’t have monopoly to suggest such
replacements - it can ba used by any tool, maybe &lt;a href=&quot;https://github.com/pinterest/ktlint&quot;&gt;ktlint&lt;/a&gt; or
&lt;a href=&quot;https://github.com/detekt/detekt&quot;&gt;detekt&lt;/a&gt; in the future.&lt;/p&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html">TL;DR: Alt+Enter is your friend when moving away from deprecated APIs in Kotlin.</summary></entry><entry><title type="html">Fact #1: Stand-alone scripts</title><link href="/blog/jekyll/update/2021/05/06/fact-1-stand-alone-scripts.html" rel="alternate" type="text/html" title="Fact #1: Stand-alone scripts" /><published>2021-05-06T08:49:47+00:00</published><updated>2021-05-06T08:49:47+00:00</updated><id>/blog/jekyll/update/2021/05/06/fact-1-stand-alone-scripts</id><content type="html" xml:base="/blog/jekyll/update/2021/05/06/fact-1-stand-alone-scripts.html">&lt;p&gt;&lt;strong&gt;TL;DR&lt;/strong&gt;: Kotlin is a good fit for everyday scripting, as a type-safe replacement for bash or Python.&lt;/p&gt;

&lt;p&gt;Did you know that Kotlin supports stand-alone scripts (files with &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;.main.kts&lt;/code&gt; extension), with proper IntelliJ support?
Some time ago one had to use an external library &lt;a href=&quot;https://github.com/holgerbrandl/kscript&quot;&gt;kscript&lt;/a&gt; to make it possible,
but with release of Kotlin 1.4.0 it seems like JetBrains got it even further than &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;kscript&lt;/code&gt;. For me it’s perfect for any
kind of everyday scripting that is beyond simple shell one-liners.&lt;/p&gt;

&lt;p&gt;For example, I wanted to have a script that get rids of local git branches that have been already gone from the remote.
Git lacks built-in local branch pruning (like &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;git fetch -p&lt;/code&gt; for remote ones), and
&lt;a href=&quot;https://stackoverflow.com/a/17029936/1102322&quot;&gt;such bash-based solutions&lt;/a&gt; are cumbersome to write and crash whenever git
prints something that is not a list of branches, unless you handle it explicitly. I used a Groovy library
&lt;a href=&quot;https://github.com/ajoberstar/grgit&quot;&gt;grgit&lt;/a&gt; (a Groovy wrapper for &lt;a href=&quot;https://www.eclipse.org/jgit/&quot;&gt;JGit&lt;/a&gt;, unfortunately
no longer maintained, but still usable) to have an object-based access to git. See
&lt;a href=&quot;https://github.com/krzema12/PersonalConfigs/blob/master/scripts/removeLocalMergedBranches.main.kts&quot;&gt;krzema12/PersonalConfigs/…/removeLocalMergedBranches.main.kts&lt;/a&gt;.
Shebang makes executing it as easy as calling &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;removeLocalMergedBranches.main.kts&lt;/code&gt; if it’s in your &lt;code class=&quot;language-plaintext highlighter-rouge&quot;&gt;PATH&lt;/code&gt;, from your git
repo.&lt;/p&gt;

&lt;p&gt;Demo:
&lt;img src=&quot;/blog/assets/kotlin-scripting-demo.png&quot; alt=&quot;Kotlin scripting demo&quot; /&gt;&lt;/p&gt;</content><author><name></name></author><category term="jekyll" /><category term="update" /><summary type="html">TL;DR: Kotlin is a good fit for everyday scripting, as a type-safe replacement for bash or Python.</summary></entry></feed>