{"id":5864,"date":"2018-02-14T21:18:48","date_gmt":"2018-02-14T21:18:48","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=5864"},"modified":"2018-02-14T21:32:59","modified_gmt":"2018-02-14T21:32:59","slug":"implicit-parameters-in-scala","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/implicit-parameters-in-scala\/","title":{"rendered":"Implicit parameters in Scala"},"content":{"rendered":"<p>Implicit parameters allow us to write code where one or more parameters can be supplied automatically by Scala in a similar way to dependency injection works.<\/p>\n<p>For example, let&#8217;s assume we have a Calculator class (below) which we want to supply to functions\/methods in our application<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nclass Calculator {\r\n  def add(a : Int, b : Int) = a + b;\r\n  def subtract(a : Int, b : Int) = a - b;\r\n}\r\n<\/pre>\n<p>In this instance I&#8217;m going create the equivalent of a singleton to the class, the val name doesn&#8217;t really matter in the code I&#8217;m demonstrating as Scala will simply locate the matching type within the MyImplicits object.<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nobject MyImplicits {\r\n  implicit val calculator = new Calculator();\r\n}\r\n<\/pre>\n<p>To use this singleton instance we simply import the MyImplicits._ into our code. Here&#8217;s an example of using such code. Notice how the calls to the <em>add<\/em> and <em>subtract<\/em> functions in the <em>main<\/em> method do not need to supply the <em>implicit<\/em> parameter. This is supplied by Scala for us. <\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\nobject Main {\r\n\r\n  import MyImplicits._\r\n\r\n  def add(implicit c : Calculator) : Unit = {\r\n    println(c.add(1, 2));\r\n  }\r\n\r\n  def subtract(a : Int, b : Int)(implicit c : Calculator) : Unit = {\r\n    println(c.subtract(a, b));\r\n  }\r\n\r\n  def main(args: Array&#x5B;String]) : Unit = {\r\n\r\n    val i = add\r\n    val s = subtract(10, 2)\r\n  }\r\n}\r\n<\/pre>\n<p>Of course, using such code means we could supply another instance of the Calculator class to either of the <em>add<\/em> or <em>subtract<\/em> methods if we wished or change the code to work with traits and implementations which would obviously allow us to import different implementations of the trait. Here&#8217;s the code from above but using a trait for the implicit argument<\/p>\n<pre class=\"brush: scala; title: ; notranslate\" title=\"\">\r\ntrait Calc {\r\n  def add(a : Int, b : Int) : Int;\r\n  def subtract(a : Int, b : Int) : Int;\r\n}\r\n\r\n\r\nclass Calculator extends Calc {\r\n  override def add(a : Int, b : Int) = a + b;\r\n  override def subtract(a : Int, b : Int) = a - b;\r\n}\r\n\r\nobject MyImplicits {\r\n  implicit val calculator = new Calculator();\r\n}\r\n\r\nobject Main {\r\n\r\n  import MyImplicits._\r\n\r\n  def add(implicit c : Calc) : Unit = {\r\n    println(c.add(1, 2));\r\n  }\r\n\r\n  def subtract(a : Int, b : Int)(implicit c : Calc) : Unit = {\r\n    println(c.subtract(a, b));\r\n  }\r\n\r\n  def main(args: Array&#x5B;String]) : Unit = {\r\n\r\n    val i = add;\r\n    val s = subtract(10, 2);\r\n  }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Implicit parameters allow us to write code where one or more parameters can be supplied automatically by Scala in a similar way to dependency injection works. For example, let&#8217;s assume we have a Calculator class (below) which we want to supply to functions\/methods in our application class Calculator { def add(a : Int, b : [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[165],"tags":[],"class_list":["post-5864","post","type-post","status-publish","format-standard","hentry","category-scala"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5864","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=5864"}],"version-history":[{"count":6,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5864\/revisions"}],"predecessor-version":[{"id":5872,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/5864\/revisions\/5872"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=5864"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=5864"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=5864"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}