【问题标题】:What does "???" stand for in Scala lang? [closed]“???”是什么意思代表 Scala 语言? [关闭]
【发布时间】:2021-06-06 23:35:54
【问题描述】:

其实我对这段代码sn-p有几个Q:

  1. '???'导致此异常?
  2. 可以指定什么来代替“???” ?
  3. 什么是'???'代表什么?
object InfixTypesHard2 extends App {
  val x0: Int --- String --- Boolean = ???
}
    
class ---[T, V](t: T, v: V)

堆栈跟踪:

Exception in thread "main" scala.NotImplementedError: an implementation is missing
    at scala.Predef$.$qmark$qmark$qmark(Predef.scala:344)
    at section3_OOP_operatorOverloading.InfixTypesHard2$.delayedEndpoint$section3_OOP_operatorOverloading$InfixTypesHard2$1(InfixTypesHard2.scala:5)
    at section3_OOP_operatorOverloading.InfixTypesHard2$delayedInit$body.apply(InfixTypesHard2.scala:3)
    at scala.Function0.apply$mcV$sp(Function0.scala:39)
    at scala.Function0.apply$mcV$sp$(Function0.scala:39)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:17)
    at scala.App.$anonfun$main$1(App.scala:76)
    at scala.App.$anonfun$main$1$adapted(App.scala:76)
    at scala.collection.IterableOnceOps.foreach(IterableOnce.scala:563)
    at scala.collection.IterableOnceOps.foreach$(IterableOnce.scala:561)
    at scala.collection.AbstractIterable.foreach(Iterable.scala:919)
    at scala.App.main(App.scala:76)
    at scala.App.main$(App.scala:74)
    at section3_OOP_operatorOverloading.InfixTypesHard2$.main(InfixTypesHard2.scala:3)
    at section3_OOP_operatorOverloading.InfixTypesHard2.main(InfixTypesHard2.scala)

【问题讨论】:

    标签: scala operator-overloading infix-operator


    【解决方案1】:

    只是一个method in Predef 从语言的角度来看并没有什么特别之处。
    作为you can see,实现只是抛出一个new NotImplementedError

    该方法的想法是您可以将其用作“实现”任何方法的填充物,而这个花哨名称的想法是它很容易被注意到,因为它的想法是??? 的任何使用都应该是临时的,并且在后面是固定的。

    如果你想知道为什么它可以在任何地方使用,那是因为??? 本身就是Nothing 类型;这是因为抛出异常的行为具有这种类型。而且由于Nothing 是所有类型 (也称为底部类型子类型,它总是可以由于Liskov而被使用。

    可以分配什么而不是'???' ?

    类型的东西:Int --- String --- Boolean;这只是一个奇怪的元组重新实现。

    // For example:
    new ---(new ---(1, "text"), false)
    // Thanks to Jasper Moeys for providing the right syntax.
    

    我想这是一些家庭作业,不知道你被要求在这里做什么。
    或者,sn-p 的整个想法是表明当类型有两个类型参数时,您可以将类型用作中缀。

    【讨论】:

    • 我的第二个问题是关于我应该使用什么而不是'???'使其编译。我试过这个 ---[---[1, "text"], true]。不幸的是,没有运气。
    • 它编译得很好。 ??? 在运行时抛出
    • @Alex_Gofman 使用圆括号代替方括号 ---(---(1, "text"), true)。方括号用于类型参数。括号用于值参数。
    • @Alex_Gofman 使用 Jasper Moeys 提供的示例编辑了答案(谢谢!)
    • 谢谢你,Jasper-M、Luis Miguel Mejía Suárez、Levi Ramsey
    猜你喜欢
    • 2014-11-07
    • 2015-03-04
    • 2011-03-12
    • 2011-05-05
    • 2020-10-30
    • 1970-01-01
    • 1970-01-01
    • 2011-08-27
    • 2023-03-27
    相关资源
    最近更新 更多