Scala和Python的通行证

Arg*_*Arg 5 scala

我想知道,有没有相当于python的传递表达式?我们的想法是编写没有实现的方法签名,并编译它们只是为了某些库原型的类型检查签名.我能够使用这种模拟这种行为:

def pass[A]:A = {throw new Exception("pass"); (new Object()).asInstanceOf[A]}
Run Code Online (Sandbox Code Playgroud)

现在我写的时候:

def foo():Int = bar()
def bar() = pass[Int]
Run Code Online (Sandbox Code Playgroud)

它的工作原理(它可以运行,但运行时爆炸,这很好),但我的实现感觉不对(例如java.lang.Object()的用法).有没有更好的方法来模拟这种行为?

Kim*_*bel 9

在Scala 2.10中,Predef中有???方法.

scala> ???
scala.NotImplementedError: an implementation is missing
  at scala.Predef$.$qmark$qmark$qmark(Predef.scala:252)
  ...
Run Code Online (Sandbox Code Playgroud)

在2.9中,您可以像这样定义自己的:

def ???[A]:A = throw new Exception("not implemented")
Run Code Online (Sandbox Code Playgroud)

如果您使用此版本而没有明确的类型参数,A则推断为Nothing.