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?
: _* 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.
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.
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.
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.
???
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With