Saravana
Saravana

Reputation: 12817

what is '???' in Scala?

I'm learning Scala using IntelliJ IDE.

When I subs class Element and override contents method, IDE provided default implementation for contents method with definition ???

Below code from the book Programming in Scala, 3rd edition

Element

abstract class Element {
  def contents: Array[String]

  def height = contents.length

  def width = if (height == 0) 0 else contents(0).length
}

ArrayElement

class ArrayElement(cont: Array[String]) extends Element {
  override def contents: Array[String] = ??? // impl provided by IDE
}

I don't see any issues in running the program but when I access the method I get below exception

Exception in thread "main" scala.NotImplementedError: an implementation is missing
    at scala.Predef$.$qmark$qmark$qmark(Predef.scala:284)
    at org.saravana.scala.ArrayElement.contents(ScalaTest.scala:65)

Can someone explain what is ??? and use of it?

Upvotes: 13

Views: 5680

Answers (1)

puhlen
puhlen

Reputation: 8529

??? is designed as a placeholder and is a method defined in Predef (which is automatically imported by default)

It's definition is

def ??? : Nothing = throw new NotImplementedError

So it has return type Nothing and all it does is throw NotImplementedError. This definition allows it to used as a placeholder implementation for methods you defined but haven't implemented yet but still want to be able to compile your program.

Nothing is a subtype of every type, which makes ??? a valid implementation no matter what type is expected.

Upvotes: 21

Related Questions