English 中文(简体)
为什么这些暗中转换导致代行编码
原标题:Why these implicit conversions resulted in looping code

考虑在Schala采用以下代码:

object Test {
  class A {}

  class B extends A {}

  class AI extends A {
    def sayHello: String = "Hello from AI"
  }

  implicit def AtoAI(a: A): AI = a

  class BI extends B {
    def sayHello: String = "Hello from BI"
  }

  implicit def BtoBI(b: B): BI = b

  def main(args: Array[String]) {
    val a = new A
    println(a.sayHello)

    val b = new B
    println(b.sayHello)
  }
}

使用含蓄物导致成形法。 事实上,拆分物显示,产生转换方法的只有goto 0。 内部:

public Test$AI AtoAI(Test$A);
  Code:
   0:   goto    0

public Test$BI BtoBI(Test$B);
  Code:
   0:   goto    0

What causes that behaviour? I understand, that a hierarchy of classes here is a suspect, but implicit conversion should be applied only once.

I use Scala 2.9.1

最佳回答
问题回答

我无法理解,为什么法典完全编纂。 我核实它确实汇编了。 tn t 你不得不在暗中转换方法中下台?

Didier points out, that the implicit conversion gets applied recursively, which means the code can compile without a downcast.

下面的法典增加了降水量(should,不改变操作时间的行为)。 失败时,它失败了。 www.un.org/Depts/DGACM/index_french.htm 正如迪迪耶所说,明文的降播阻止了在这里重新适用默示转换。

object Test {
  class A {}

  class B extends A {}

  class AI extends A {
    def sayHello: String = "Hello from AI"
  }

  implicit def AtoAI(a: A): AI = a.asInstanceOf[AI]

  class BI extends B {
    def sayHello: String = "Hello from BI"
  }

  implicit def BtoBI(b: B): BI = b.asInstanceOf[BI]

  def main(args: Array[String]) {
    val a = new A
    println(a.sayHello)

    val b = new B
    println(b.sayHello)
  }
}

www.un.org/Depts/DGACM/index_french.htm A, 即无这种方法。 显然,这会造成工作瘫痪。 发生的情况没有说明;就你而言,这是一种无限的循环。





相关问题
How to flatten a List of different types in Scala?

I have 4 elements:List[List[Object]] (Objects are different in each element) that I want to zip so that I can have a List[List[obj1],List[obj2],List[obj3],List[obj4]] I tried to zip them and I ...

To use or not to use Scala for new Java projects? [closed]

I m impressed with Twitter and investigating to use Scala for a new large scale web project with Hibernate and Wicket. What do you think about Scala, and should I use it instead of Java? EDIT: And, ...

Why does Scala create a ~/tmp directory when I run a script?

When I execute a Scala script from the command line, a directory named "tmp" is created in my home directory. It is always empty, so I simply deleted it without any apparent problem. Of course, when I ...

Include jar file in Scala interpreter

Is it possible to include a jar file run running the Scala interpreter? My code is working when I compile from scalac: scalac script.scala -classpath *.jar But I would like to be able to include a ...

Scala and tail recursion

There are various answers on Stack Overflow which explain the conditions under which tail recursion is possible in Scala. I understand the limitations and how and where I can take advantage of tail ...

热门标签