Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 476
Saravana Avatar asked Dec 08 '17 15:12

Saravana


People also ask

What does _* mean in Scala?

: _* is a special instance of type ascription which tells the compiler to treat a single argument of a sequence type as a variable argument sequence, i.e. varargs.

What is with in Scala?

Use the with Keyword in ScalaThis keyword is usually used when dealing with class compositions with mixins. Mixins are traits that are used to compose a class. This is somewhat similar to a Java class that can implement an interface.

What is a Scala symbol?

Scala has what are called symbolic literals. A symbol is very similar to a String except that they are cached. So symbol 'hi will be the same object as 'hi declared a second time. In addition there is special syntax for creating symbols that requires only a single quote.

Is Scala harder than Java?

7- The learning curve of the Scala vs java programming language is high as compared to that of Java. The coding in Scala can be extremely challenging to predict due to less coding. Also, the syntax in Scala is more complicated than Java.


1 Answers

??? 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.

like image 133
puhlen Avatar answered Sep 23 '22 00:09

puhlen