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 976397
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:44:25+00:00 2026-05-16T03:44:25+00:00

I know that type erasure makes them look equal, type-wise, at runtime, so that:

  • 0

I know that type erasure makes them look equal, type-wise, at runtime, so that:

class Bar {
    def foo[A](xs: A*) { xs.foreach(println) }
    def foo[A, B](xs: (A, B)*) { xs.foreach(x => println(x._1 + " - " + x._2)) }
}   

gives the following compiler error:

<console>:7: error: double definition:
method foo:[A,B](xs: (A, B)*)Unit and
method foo:[A](xs: A*)Unit at line 6
have same type after erasure: (xs: Seq)Unit
        def foo[A,B](xs: (A, B)*) { xs.foreach(x => println(x._1 + " - " + x._2)
) }
            ^

But is there a simple way to be able to write:

bar.foo(1, 2, 3)
bar.foo(1 -> 2, 3 -> 4)

and having these call different overloaded versions of foo, without having to explicitly name them:

bar.fooInts(1, 2, 3)
bar.fooPairs(1 -> 2, 3 -> 4)
  • 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-16T03:44:25+00:00Added an answer on May 16, 2026 at 3:44 am

    You can, in a fairly round about way. Foo is a type class, and the compiler implcitly passes an instance of the type class, compatible with the (inferred) type parameter A.

    trait Foo[X] {
      def apply(xs: Seq[X]): Unit
    }
    
    object Foo {
     implicit def FooAny[A]: Foo[A] = new Foo[A] {
        def apply(xs: Seq[A]) = println("apply(xs: Seq[A])")
      }
      implicit def FooTuple2[A, B]: Foo[(A, B)] = new Foo[(A, B)] {
        def apply(xs: Seq[(A, B)]) = println("apply(xs: Seq[(A, B)])")
      }
    
      def apply[A](xs: A*)(implicit f: Foo[A]) = f(xs)
    }
    
    
    Foo(1, 2, 3)        // apply(xs: Seq[A])
    Foo(1 -> 2, 2 -> 3) // apply(xs: Seq[(A, B)])
    

    In the second call, both FooAny and FooTuple2 could be passed, but the compiler picks FooTuple2, based on the rules of static method overloading. FooTuple2 is considered more specific that FooAny. If two candidates are considered to be as specific as each other, an ambiguity error is raised. You can also prefer one over the other by placing one in a superclass, as is done in scala.LowPriorityImplicits.

    UPDATE

    Riffing off the DummyImplicit idea, and the followup thread on scala-user:

    trait __[+_]
    object __ {
     implicit object __ extends __[Any]
    }
    
    object overload {
     def foo(a: Seq[Boolean]) = 0
    
     def foo[_: __](a: Seq[Int]) = 1
    
     def foo[_: __ : __](a: Seq[String]) = 2
    }
    
    import overload._
    foo(Seq(true)) 
    foo(Seq(1)) 
    foo(Seq("s")) 
    

    This declares a type-parameterized trait __, covariant in its unnamed type parameter _. Its companion object __ contains an implicit instance of __[Any], which we’ll need later on. The second and third overloads of foo include a dummy type parameters, again unnamed. This will be inferred as Any. This type parameter has one or more context bounds, which are desugared into additional implicit parameters, for example:

     def foo[A](a: Seq[Int])(implicit ev$1: __[A]) = 1
    

    The multiple parameter lists are concatenated into a single parameter list in the bytecode, so the double definition problem is circumvented.

    Please consider this as a opportunity to learn about erasure, context bounds and implicit search, rather than as a pattern to be applied in real code!

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

Sidebar

Related Questions

I came to know that Grails service class are of Singleton type. For what
I know that non-type template argument for intgral type must be const expression so:
I know that such type of questions exist in SF but they are very
I know that the builtin set type in python is not generally threadsafe, but
I know that there is no return type of the constructors in C++ However,
We know that string is a reference type , so we have string s=God
I know that generics are used to achieve type safety and i frequently read
I know that SQL Server does not have boolean data type and your best
I know that in Java you can specify the return type of the DoInBackground
I know for a matter of fact that Type cannot be used when passing

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.