Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7055881
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:48:05+00:00 2026-05-28T03:48:05+00:00

Consider following code in Scala: object Test { class A {} class B extends

  • 0

Consider following code in Scala:

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)
  }
}

The use of implicits results in looping code. In fact, disassemblying reveals, that generated conversion methods have only a goto 0 inside:

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T03:48:06+00:00Added an answer on May 28, 2026 at 3:48 am

    Not nice but I would definitely not call it a bug.

    It boils down to

    class A
    
    class B
    
    implicit def aToB(a: A) : B = a
    

    There is no need for the two sides of the conversion to be related in any way. The implicit is just the same thing as writing

    implicit def aToB(a: A): B = aToB(a)
    

    because the compiler inserts the aToB call in order to convert the result a to the required return type B.

    The goto 0 implementation is just a tail call optimization. The compiler might probably gives a warning when it generates a method that starts this way.

    Maybe there could be a rule that implicit methods are not available as implicits inside their own body. But it does not always create an infinite loop

    implicit def listAToListB(l: list[A] = l match {
      case Nil => Nil
      case x:xs => toB(x) :: xs // equivalent to toB(x) :: listAToList[B](xs)
    }
    

    (ok this is just a map(toB)). Anyway the same could happen with two mutually recursive implicits. In my opinion, it’s not worthwhile to tweak the spec just to avoid some possibility to write an infinite, do nothing loop, among many others. But a warning when such a loop is detected, irrespective of implicits, would be nice.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Consider the following Scala code: object MainObject { def main(args: Array[String]) { import Integer.{
Consider the following code: def create_class(class_name, superclass, &block) klass = Class.new superclass, &block Object.const_set
Consider the following Scala code: package scala_java object MyScala { def setFunc(func: Int =>
Consider the following code: class Base(object): @classmethod def do(cls, a): print cls, a class
Consider the following Scala code (e.g., in REPL) object A{def foo:Unit = {}} object
Consider the following piece of code: case class User(id: Int, name: String) object User{
Consider the following Scala code def NOTImplementedIn[T<: AnyRef](t:T):String = throw new Exception(t.getClass.getName+: Input type
Consider the following typical Scala 'pimp' code: class PimpedA(a:A){ def pimp() = hi }
Consider the following code snippet below. class X { public String toString() { return
Consider the following two fragments of code: scala> def f1(x:Any) = x match {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.