当 x.toString 失败时,ScalaRunTime.stringOf(x) 如何不失败? [英] How is ScalaRunTime.stringOf(x) not failing when x.toString fails?

查看:65
本文介绍了当 x.toString 失败时,ScalaRunTime.stringOf(x) 如何不失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在试图找出一些 joda-time DateTime(时间戳格式)问题的同时,我用

Whilst trying to figure out some joda-time DateTime (timestamp formatting) issues I opened a REPL with

scala -cp joda-time-2.3.jar

忘记添加joda-convert jar,最后得到一个

and forgot to add the joda-convert jar, and eventually got a

java.lang.AssertionError: assertion failed: org.joda.convert.ToString

(整个堆栈跟踪)

我能够将其简化为:

> scala -cp joda-time-2.3.jar
Welcome to Scala version 2.11.0 (Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0_05).
Type in expressions to have them evaluated.
Type :help for more information.

scala> val dt = new org.joda.time.DateTime
warning: Class org.joda.convert.FromString not found - continuing with a stub.
warning: Class org.joda.convert.ToString not found - continuing with a stub.
warning: Class org.joda.convert.FromString not found - continuing with a stub.
warning: Class org.joda.convert.ToString not found - continuing with a stub.
dt: org.joda.time.DateTime = 2014-05-14T17:54:24.511+01:00

scala> scala.runtime.ScalaRunTime.stringOf(dt)
res0: String = 2014-05-14T17:54:24.511+01:00

scala> dt.toString
java.lang.AssertionError: assertion failed: org.joda.convert.ToString

ScalaRunTime.stringOf(dt) 如何在 dt.toString 失败的情况下成功?

How is ScalaRunTime.stringOf(dt) succeeding where dt.toString fails?

推荐答案

您没有发布堆栈跟踪,这是编译器崩溃而不是 joda 的失败断言.

You didn't post the stack trace, which is a compiler crash and not a failed assertion by joda.

REPL 在编译表达式时崩溃.

The REPL is crashing compiling the expression.

看起来 AbstractDateTime 有一个重载的 toString 方法,并且重载解析在通常的 toString 上的 @ToString 注释上崩溃().(ToString 的符号丢失.)

It looks like AbstractDateTime has an overloaded toString method, and overload resolution crashes on the @ToString annotation on the usual toString(). (The symbol for ToString is missing.)

但是stringOf(x: Any)当然只是在调用Object.toString().

显然有一些已知问题.最近的问题已修复.

在 2.10.4 上:

On 2.10.4:

scala> (dt: Any).toString
res0: String = 2014-05-14T11:56:21.794-07:00

scala> dt.toString
<console>:9: error: ambiguous reference to overloaded definition,
both method toString in class AbstractDateTime of type (x$1: String, x$2: java.util.Locale)String
and  method toString in class AbstractDateTime of type (x$1: String)String
match expected type ?
              dt.toString
                 ^

scala> dt.toString()  // crashes

2.10.3 比较颠簸,并声称 加载 DateTime 时出错,类文件损坏.

2.10.3 is bumpier and claims error while loading DateTime, class file is broken.

崩溃发生在 2.11.0.

The crash is on 2.11.0.

这篇关于当 x.toString 失败时,ScalaRunTime.stringOf(x) 如何不失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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