什么是 '???'在斯卡拉吗?

编程入门 行业动态 更新时间:2024-10-11 05:21:50
本文介绍了什么是 '???'在斯卡拉吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用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.

更多推荐

什么是 '???'在斯卡拉吗?

本文发布于:2023-07-15 13:16:59,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1111432.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:斯卡

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!