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

  • SEARCH
  • Home
  • 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 3224506
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:13:53+00:00 2026-05-17T16:13:53+00:00

In Scala, the interaction of overloading and implicit argument resolution seem to make it

  • 0

In Scala, the interaction of overloading and implicit argument resolution seem to make it impossible to make the following code usable.

trait Bijection[A, B] extends Function1[A, B] with Unapply[A, B] { self =>
  def apply(a: A): B
  def unapply(b: B): A
}

sealed trait Unapply[A, B] {
  def unapply(b: B): A
}

object Bijection {
  implicit def biject[A](a: A): Biject[A] = new Biject(a)

  implicit object IntStringBijection extends Bijection[Int, String] {
    override def apply(a: Int): String = a.toString
    override def unapply(b: String): Int = b.toInt
  }
}

sealed class Biject[A](a: A) {
  def as[B](implicit f: Function1[A, B]): B = f(a)
  def as[B](implicit f: Unapply[B, A]): B = f unapply a
}

The goal here is for a.as[B] to perform a typesafe conversion irrespective of whether a Bijection[A,B] or a Bijection[B,A] is available in implicit scope.

The reason that this doesn’t work is that implicit resolution appears to take place after overload disambiguation in the compiler, and since both implementations of ‘as’ have the same result type, the compiler doesn’t even get around to attempting to find out whether an appropriate implicit is in scope that can perform the conversion. In short, implicit resolution is not used in overload disambiguation.

The reason I want to have ‘as’ overloaded is to avoid the need for the user of this library to need to encode the “direction” of the bijection at the call site; obviously one could implement Biject as this:

sealed class Biject[A](a: A) {
  def viaForward[B](implicit f: Function1[A, B]): B = f(a)
  def viaReverse[B](implicit f: Unapply[B, A]): B = f unapply a
}

but this is really unappealing because it essentially makes the pimp superfluous; one might as well explicitly pass the bijection, but then of course you lose the ability to have the bijection that is used vary based upon the scope.

Is there any good solution to this problem?

  • 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-17T16:13:53+00:00Added an answer on May 17, 2026 at 4:13 pm

    How’s this?

    trait Bijection[A, B] extends Function1[A, B] with Unapply[A, B] {
      self =>
      def apply(a: A): B
    
      def unapply(b: B): A
    }
    
    sealed trait Unapply[A, B] {
      def unapply(b: B): A
    }
    
    object Bijection {
      implicit def biject[A](a: A): Biject[A] = new Biject(a)
    
      implicit object IntStringBijection extends Bijection[Int, String] {
        override def apply(a: Int): String = a.toString
    
        override def unapply(b: String): Int = b.toInt
      }
    }
    
    sealed class Biject[A](a: A) {
      def as[B](implicit f: Either[Bijection[A, B], Bijection[B, A]]): B = f.fold(_ apply a, _ unapply a)
    }
    
    trait EitherLow {
      implicit def left[A, B](implicit a: A): Either[A, B] = Left(a)
    }
    
    object Either extends EitherLow {
      implicit def right[A, B](implicit b: B): Either[A, B] = Right(b)
    }
    
    import Bijection._
    import Either._
    
    1.as[String]
    "1".as[Int]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

scala> class A defined class A scala> trait B defined trait B Creating an
In Scala, I can make a caseclass, case class Foo(x:Int) , and then put
Since I want to generate Scala code from an annotated Scala class, I need
I have a file with several lines of scala code -- imports, list value
I have a simple piece of Scala code. I loop sequentially through a List
I'm following the Scala, Eclipse and Maven Integration tutorial . It is really useful.
I'm running this scala code on a 32-bit quad-core Core2 system: def job(i:Int,s:Int):Long =
scala> import scala.collection.mutable.Buffer import scala.collection.mutable.Buffer scala> val b = Buffer(1, 2, 3) b: scala.collection.mutable.Buffer[Int]
scala> class A (s: String*) { val l: ListBuffer[String] = ListBuffer[String](s) } <console>:8: error:
scala> Random.shuffle((1 to 10).toSet) res10: scala.collection.immutable.Set[Int] = Set(5, 10, 1, 6, 9, 2, 7,

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.