不建议使用Scala错误功能.有什么选择? [英] Scala error function deprecated. What is the alternative?

查看:55
本文介绍了不建议使用Scala错误功能.有什么选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将一些Haskell代码移植到Scala中.在Haskell中,我可以使用 error 函数.似乎可以在Scala中执行此操作,但是IDE向我显示此功能已被弃用.这是代码:

I am porting some Haskell code over into Scala. In Haskell I can use the error function. It seems at some point you could do this in Scala but the IDE is showing me that this is deprecated now. Here is the code:

def prime (n : Int) : Boolean = () match {
    case _ if n < 1 => error("not a positive integer")
    case _ if n == 1 => false
    case _ => ld (n) == n
}

现在我应该使用什么代替Scala中的错误功能?

What do I use instead of the error function in Scala now?

推荐答案

您应将 sys.error 用作

You should use sys.error as mentioned in deprecated message.

@deprecated("Use `sys.error(message)` instead", "2.9.0")

您可以使用 -deprecation 选项运行scala以获得此消息:

You could run scala with -deprecation option to get this message:

scala> def t = error("t")
<console>:7: warning: method error in object Predef is deprecated: Use `sys.error(message)` instead

这篇关于不建议使用Scala错误功能.有什么选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆