How does ScalaRunTime.stringOf (x) not work when x.toString fails?

While trying to find out some problems with joda-time DateTime(timestamp formatting) I opened REPL with

scala -cp joda-time-2.3.jar

and forgot to add the jar joda-convertand eventually got

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

( Whole stacktrace file )

I was able to simplify this:

> 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

How does it ScalaRunTime.stringOf(dt)succeed, where does it dt.toStringfail?

+4
source share
1 answer

You did not post a stack trace, which is a compiler failure, and not a failed statement from joda.

REPL expression compilation failed.

, AbstractDateTime toString, @ToString toString(). ( toString .)

stringOf(x: Any) Object.toString(), .

, , . A .

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 error while loading DateTime, class file is broken.

2.11.0.

+7

Source: https://habr.com/ru/post/1540560/


All Articles