Search code examples
scalapattern-matchingellipsis

Illegal Start of Simple Expression: Option Type and Ellipsis


New to Scala and just began the scala.Option Cheat Sheet. However, this code is throwing an error in the sbt console.

def option[A, X](o: Option[A])(none: => X, some: => A => X): X = ...

The error is

error: illegal start of simple expression

the up-arrow points to the ellipsis. The fix seems simple, but as a newbie, it currently eludes me


Solution

  • ... is not a valid Scala expression. If you want a function with an "unknown" implementation you can use ???:

    def option[A, X](o: Option[A])(none: => X, some: => A => X): X = ???
    

    The goal of this function is apparently to take a function as parameter and to apply either none or some depending on the content of the option. You can implement it using pattern matching:

    def option[A, X](o: Option[A])(none: => X, some: => A => X): X = o match {
      case Some(a) => some(a)
      case None => none
    }