scalapartialfunctionlifting

Scala: lift on Array


So I have an array and I can do:

myArr.lift(0)

...and it gives me option of the value at index 0.

So what actually is happening here? When I try to go to lift definition, IDE takes me to PartialFunction, and I see Array doesn't inherit from it.

And whats the usecase for using lift?


Solution

  • It is true that Array is primitive native to JVM and doesn't have any lift method.

    However in Scala we have implicit conversions and extension methods and this is what we expect to find when we are able to call methods not defined for some type.

    If you write the code in IntelliJ and use a magic shortcut for showing implicits (also available in metals)you'll see that your code is turned into e.g (assuming array of ints):

    wrapIntArray(myArr).lift(0)
    

    where wrapIntArray is defined in scala.Predef (see Array to ArraySeq section) as

    implicit def wrapIntArray(xs: Array[Int]): WrappedArray[Int] = if (xs ne null) new WrappedArray.ofInt(xs) else null
    

    (in Scala 2.13 WrappedArray is deprecated and became aliased to ArraySeq).

    If you are curious why it is in your scope remember that by default Scala compiler imports all definitions from the following paths:

    (I wrote "default" because you can change it with -Yimports but custom predef is something I wouldn't recommend if you don't have experienced devs and good documentation around).

    What is the use case of lift? Basically all Scala collections share some common traits and PartialFunction is one of them:

    Reasoning is pretty simple - if you are accessing values in collection like coll(index) you are using it like function, but since it is not a total mapping it should be a partial function.

    For PartialFunctions lift is just a nice utility to return Option[Value] instead of Value or Exception on apply, that you just got because WrappedArray like any other collection inherit from it.