开发者

Use function without explicit import in Scala

I like Scala's sys.error func开发者_Go百科tion - but I want to distinguish two cases: internal errors (e.g. database problem) and user errors (invalid input).

I tried extending Scala - but it doesn't seem to work:

package scala

class UserException(msg: String) extends RuntimeException(msg)

package object err {

    def internal(message: String): Nothing =
        sys.error(message)

    def usr(message: String): Nothing =
        throw new UserException(message)
}

How should I define err.usr() to be able to use it without an explicit import?


You can't, only scala.Predef is imported by default and it's not user extensible in any useful way.


You could put these definitions on the package object of your package hierarchy. Then, everything on that package will see them without import.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜