We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies.

We use cookies and other tracking technologies to improve your browsing experience on our site, analyze site traffic, and understand where our audience is coming from. To find out more, please read our privacy policy.

By choosing 'I Accept', you consent to our use of cookies and other tracking technologies. Less

We use cookies and other tracking technologies... More

Login or register
to apply for this job!

Login or register
to save this job!

Login or register to start contributing with an article!

Login or register
to see more jobs from this company!

Login or register
to boost this post!

Show some love to the author of this blog by giving their post some rocket fuel 🚀.

Login or register to search for your ideal job!

Login or register to start working on this issue!

Engineers who find a new job through Functional Works average a 15% increase in salary 🚀

Blog hero image

Implicit Conversions in Scala

ℜodrigo Arĥimedeς ℳontegasppα ℭacilhας 24 December, 2020 | 2 min read

Scala (in version 2.13 while I write) has a powerful conversion system based on its implicits system.

It works by expanding implicit methods and classes into a more complex structure, which would be way harder to code if it needed to be done explicitly.

We’re gonna talk about three ways to implement implicit conversions and their expansions.

Implicit methods

Take the double value sequence:

Seq(0.4, 1.8, 2.2)

In this case it’s a short list, but it could be hundreds larger. One needs only their ceiling integer values, so one must a way to convert the values.

One can use an implicit method:

implicit def double2int(value: Double): Int = value.ceil.toInt

val values: Seq[Int] = Seq(0.4, 1.8, 2.2)

The value of values is:

Seq(1, 2, 3)

It’s expanded to:

def double2int(value: Double): Int = value.ceil.toInt

val values: Seq[Int] = Seq(0.4, 1.8, 2.2) map double2int

Implicit classes

Implicit classes are the way to inject methods into existent types.

For instance, let’s create a string method to generate the XML node from it:

implicit class XMLString(value: String) {
  def toXML: Option[NodeSeq] = Try(XML loadString value).toOption
}

val source = getDataFromOutsideSource() // : String

val node = source.toXML

It expands to:

class XMLString(value: String) {
  def toXML: Option[NodeSeq] = Try(XML loadString value).toOption
}

object XMLString extends (String => XMLString) {
  def apply(value: String): XMLString = new XMLString(value)
}

val source: String = getDataFromOutsideSource()

val node: Option[NodeSeq] = XMLString(source).toXML

Note that everytime .toXML is called, a new XMLString is created.

Implicit value classes

Value classes are lightweight Scala resources, which don’t create new instance each call. Instead, every value class uses a companion object to envelope the methods, and calls them from it.

Consider the following example, a method to determine whether a double is integral:

implicit class IntegralDouble(val value: Double): extends AnyVal {
  def isIntegral: Boolean = value % 1 == 0
}

val value = getSomeFloatPointValue() // : Double

if (value.isIntegral)
  doSomeMathWith(value)

Which expands to:

class IntegralDouble(val value: Double): extends AnyVal {
  def isIntegral: Boolean = IntegralDouble isIntegral$expansion value
}

object IntegralDouble {
  def isIntegral$expansion(value: Double): Boolean = value % 1.0 == 0.0
}

val value: Double = getSomeFloatPointValue()

if (IntegralDouble.isIntegral$expansion(value))
  doSomeMathWith(value)

No instance is created on .isIntegral call.

Note: value classes are tagged by inheriting AnyVal, and need a value of type * <: AnyVal, i.e., Boolean, Byte, Char, Double, Float, Int, Long, Short, Unit, and their literal-based singleton types.

Originally published on kodumaro.cacilhas.info

Author's avatar
ℜodrigo Arĥimedeς ℳontegasppα ℭacilhας
My name is Legion, for we are many.
    Python
    Java
    C++
    Go
    Node.js
    TypeScript
    Perl
    Erlang
    Haskell
    Ruby
    Lua
    Moonscript
    Scheme
    Julia
    Smalltalk

Related Issues

WorksHub / client
  • Started
  • 0
  • 16
  • Intermediate
  • Clojure
  • $150
viebel / klipse-clj
viebel / klipse-clj
  • Open
  • 0
  • 0
  • Intermediate
  • Clojure
viebel / klipse
  • Open
  • 0
  • 0
  • Intermediate
  • Clojure
viebel / klipse
  • 1
  • 0
  • Intermediate
  • Clojure
viebel / klipse
  • Started
  • 0
  • 2
  • Intermediate
  • Clojure
  • $80
viebel / klipse
  • Open
  • 0
  • 0
  • Advanced
  • Clojure
  • $80
viebel / klipse
  • Started
  • 0
  • 2
  • Advanced
  • Clojure
  • $180
viebel / klipse
  • Started
  • 0
  • 1
  • Intermediate
  • Clojure
viebel / klipse
  • 1
  • 1
  • Advanced
  • Clojure
  • $300

Get hired!

Sign up now and apply for roles at companies that interest you.

Engineers who find a new job through Functional Works average a 15% increase in salary.

Start with GitHubStart with Stack OverflowStart with Email