问题描述
我正在使用IntelliJ
IDE学习Scala
.
I'm learning Scala
using IntelliJ
IDE.
当我继承类Element
并重写contents
方法时,IDE为定义为???
的contents
方法提供了默认实现.
When I subs class Element
and override contents
method, IDE provided default implementation for contents
method with definition ???
本书Programming in Scala, 3rd edition
元素
abstract class Element {
def contents: Array[String]
def height = contents.length
def width = if (height == 0) 0 else contents(0).length
}
ArrayElement
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?
推荐答案
???
被设计为占位符,并且是Predef
(默认情况下自动导入)中定义的方法
???
is designed as a placeholder and is a method defined in Predef
(which is automatically imported by default)
它的定义是
def ??? : Nothing = throw new NotImplementedError
因此它的返回类型为Nothing
,而它所做的只是抛出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
是每种类型的子类型,无论期望哪种类型,???
都是有效的实现.
Nothing
is a subtype of every type, which makes ???
a valid implementation no matter what type is expected.
这篇关于什么是 '???'在斯卡拉吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!