<?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 Sid on Medium]]></title>
        <description><![CDATA[Stories by Sid on Medium]]></description>
        <link>https://medium.com/@siddartha-errabolu?source=rss-167ee02a7aba------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Zk-PihQSTvA3Rxd6.jpg</url>
            <title>Stories by Sid on Medium</title>
            <link>https://medium.com/@siddartha-errabolu?source=rss-167ee02a7aba------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 05:08:10 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@siddartha-errabolu/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[Scala Implicits — Journey Begins]]></title>
            <link>https://siddartha-errabolu.medium.com/scala-implicits-journey-begins-e150b7bbdcf5?source=rss-167ee02a7aba------2</link>
            <guid isPermaLink="false">https://medium.com/p/e150b7bbdcf5</guid>
            <dc:creator><![CDATA[Sid]]></dc:creator>
            <pubDate>Fri, 12 Feb 2021 13:53:38 GMT</pubDate>
            <atom:updated>2021-02-13T12:05:02.358Z</atom:updated>
            <content:encoded><![CDATA[<h3>Scala Implicits — Journey Begins</h3><p>This article covers the 2 basic concepts of Scala Implicits. Implicit parameters and implicit conversions</p><h3>Before you read</h3><p>I am assuming that you know basic Scala Programming, Scala Type Bounds (aka Generics)</p><h3>Implicit definition</h3><p>Here what Scala docs say</p><blockquote>Implicits in Scala refers to either a value that can be passed “automatically”, so to speak, or a conversion from one type to another that is made automatically.<br>That’s what Scala exactly does</blockquote><blockquote><a href="https://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html">https://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html</a></blockquote><p>Scala has</p><ul><li>implicit parameters</li><li>implicit conversions</li></ul><p>The context in which they are used are entirely different. Lets explore each of them in detail</p><h4>Implicit parameters</h4><p>Firstly, the implicit parameters<br>Lets start with a simple example. A method that calculates the area of a circle</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/b4fa68c450fe85ce3f5de74534291ca7/href">https://medium.com/media/b4fa68c450fe85ce3f5de74534291ca7/href</a></iframe><p>Let us take another method that calculates the circumference of a circle</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/ec9e8f9620d1fc312b296f696c3900b2/href">https://medium.com/media/ec9e8f9620d1fc312b296f696c3900b2/href</a></iframe><p>In the above two code snippets you can see that I must pass the radius and the value of pi.<br>Let us send pi Value as an implicit variable. We slightly change the above methods like this</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/50012cb3117e0e2b0644db0c3e8eb466/href">https://medium.com/media/50012cb3117e0e2b0644db0c3e8eb466/href</a></iframe><p>Now, below is the complete code snippet</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/72a4b9807cef5d8eec267d22731f5d49/href">https://medium.com/media/72a4b9807cef5d8eec267d22731f5d49/href</a></iframe><p>In the above code snippet, you can see that we declared the piValue as an implicit value. And since both methods expect pi Value as an implicit variable, Scala compiler automatically uses approxPiValue and passes it as piValue while calling these methods.</p><p>Remember that Scala will use any double value that is in its scope as an implicit value. And in this case it is approxPiValue</p><p>Also, in the above example, I can replace approxPiValue with an implicit method that returns double value</p><blockquote>implicit def calculatePIValue: Double = 22d/7d</blockquote><p>Scala compiler will now resolve piValue from the above value returned by calculatePIValue.</p><h3>How does Scala Compiler resolve an implicit parameter?</h3><p>Now, the question is, where does Scala look for implicit parameters? In our above example, there is no ambiguity since we defined the implicit value in the same method. But in practicality it does not make sense defining implicit values in the same method.</p><p>Scala compiler tries to find the value in lexical scope failing which it tries next on the implicit scope</p><h3>Lexical scope</h3><p>Scala compiler will check for a implicit value of the same type<br>- In the same class or object<br>- You can also bring implicit values defined in an Object or a trait using import statement</p><h3>Implicit scope</h3><p>Scala compiler will next search for implicit values defined in the companion object</p><p>What will happen when Scala compiler finds multiple values in lexical or implicit scope? Well, it uses overload resolution algorithm to pick one of them. If the compiler is unable to determine the implicit value, an error is thrown. Let me keep the article simple and assume that compiler finds an implicit value in either of the scopes</p><p>We covered implicit parameters because it’s the simplest form of implicit. Let us now get into implicit conversions</p><h3>Implicit conversions</h3><p>Let us take a different example. Lets say that I want to code a method called <em>vulcanGreeting</em>. This <em>vulcanGreeting</em> will take an argument *name* and prints the greeting message. Something simple like this,</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e6f309e13838dc12564050951fac6e7a/href">https://medium.com/media/e6f309e13838dc12564050951fac6e7a/href</a></iframe><p>Let’s do the above example differently. Lets use implicit conversion</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3cfe514a4ae9bd2a331990ae6a9170ba/href">https://medium.com/media/3cfe514a4ae9bd2a331990ae6a9170ba/href</a></iframe><p>Both the above examples give the same output on the console</p><blockquote>Live long and prosper… Kirk</blockquote><p>But wait, how am I able to call a method *greetInVulcan* on a String(“Kirk”). There is no such method in String. Welcome to the world of implicit conversions<br>Let’s first analyse the below object</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/3a78832d52570b982684519a3125a4b2/href">https://medium.com/media/3a78832d52570b982684519a3125a4b2/href</a></iframe><p>The <strong>Greetings</strong> object defines an implicit class <strong>VulcanGreeting</strong>. This implicit class takes an argument <strong>name</strong>. Nothing fancy about this class except for the keyword <strong>implicit</strong><br>Now let’s examine the second object</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2e994e21a2d28753207002f3ed245e08/href">https://medium.com/media/2e994e21a2d28753207002f3ed245e08/href</a></iframe><p>The line <strong>import Greetings._</strong> imports all the implicit classes in the object Greetings. Or in other words, the moment you import it, the implicit class <strong>VulcanGreeting</strong> comes into the lexical scope.<br>Now examine the line<br><strong>”Kirk”.greetInVulcan</strong><br>Since the Scala compiler has <strong>VulcanGreeting</strong> in the scope, it converts <strong>String</strong> to <strong>VulcanGreeting</strong>. And allows you to use all the methods in the VulcanGreeting on the String</p><p>Another lesser used way of implicit conversion is through implicit methods</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/6df19526c54abd4cde7ddf41c83bd01c/href">https://medium.com/media/6df19526c54abd4cde7ddf41c83bd01c/href</a></iframe><p>In line 2, you have an implicit method that takes an Int and returns a String. Hence, you can call the methods of the String on an Int as shown in Line 4.</p><p>The above example gives an output</p><blockquote>LIVE LONG AND PROSPER 4</blockquote><p>Implicit are extensively used in Scala. Understanding implicit is also the first step to understand libraries like Cats and Scalaz</p><h3>The journey from here</h3><p>When I started using Cats, I realized the path taken to get a gasp of it has been long and challenging. <br>Cats is a wonderful collection of functional programming tools. Type classes, the corner stone of Cats, is a programming pattern borrowed from Haskell. <br>The first step in this journey is to know about Scala Implicit. My objective is to simplify this journey to Functional programming excellence</p><h3>References</h3><ul><li><a href="https://docs.scala-lang.org/tutorials/FAQ/finding-implicits.html#implicit-conversion">Where does Scala look for implicits?</a></li><li><a href="https://typelevel.org/cats/">Cats: Home</a></li><li><a href="https://www.amazon.com/Functional-Programming-Simplified-Alvin-Alexander-ebook/dp/B076J7CJKY/ref=tmm_kin_swatch_0?_encoding=UTF8&amp;qid=1613042253&amp;sr=1-3">Functional Programming, Simplified: (Scala edition)</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=e150b7bbdcf5" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>