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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:51:33+00:00 2026-05-26T19:51:33+00:00

Stumbled over that def foo(f: Int => Unit) {} def foo(f: Long => Unit)

  • 0

Stumbled over that

def foo(f: Int => Unit) {}
def foo(f: Long => Unit) {}

doesn’t compile because of method foo is defined twice. I know that above is only a shorthand for

def foo(f: Function1[Int, Unit]) {}
def foo(f: Function1[Long, Unit]) {}

and that after type erasure both methods have same signature.

Now I’ve read in Try out specialized Function1/Function2 in 2.8.0 RC1! that Function1 and Function2 have @specialized versions for Int, Long and Double since Scala 2.8. That surely means that Function[Int, Unit] and Function[Long, Unit] have separate class files at JVM level.

Would then not both signatures are different?

Is the problem, that second type parameter will continue to be erased? But same problem with

class Bar[@specialized T]
def foo(f: Bar[Int]) {}
def foo(f: Bar[Long]) {}

it doesn’t compile.

  • 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-26T19:51:33+00:00Added an answer on May 26, 2026 at 7:51 pm

    @specialized has nothing to do with type erasure, at least in this case. It means that an extra version of your class is generated with the native type in the position. This saves on boxing/unboxing notably.

    So you define a class like:

    class MyClass[@specialized(Int) T] {
      def foobar(t: T) = {}
    }
    

    and you get two classes as output, (approximately):

    class Foobar[java.lang.Object] {
      def foobar(t: java.lang.Object) = {}
    }
    
    class Foobar[int] {
      def foobar(t: int) = {}
    }
    

    You need to have two implementations of the class because you can’t always guarantee that the one with the correct native type will be called. The scala compiler will choose which one to call. Note that the java compiler has no idea this specialization is taking place, so must call the unspecialized methods.

    In fact, the output is the following (via JAD):

    public class MyClass implements ScalaObject {
        public void foobar(Object obj) { }
    
        public void foobar$mcI$sp(int t) {
            foobar(BoxesRunTime.boxToInteger(t));
        }
    
        public MyClass() { }
    }
    
    public class MyClass$mcI$sp extends MyClass {
        public void foobar(int t) {
            foobar$mcI$sp(t);
        }
    
        public void foobar$mcI$sp(int i) { }
    
        public volatile void foobar(Object t) {
          foobar(BoxesRunTime.unboxToInt(t));
        }
    
        public MyClass$mcI$sp() {}
    }
    

    So your type erasure problem will not be fixed with @specialized.

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

Sidebar

Related Questions

I stumbled over some code that appears to be correct. It is supposed to
we recently stumbled over a behaviour of NHibernate that gave us a real headache
I recently stumbled over this post , which introduces the collect method for Scala
Recently I stumbled over code such as this: void foo(const Bar* b) { ...
Yesterday I stumbled over the information that Crystal Reports will no longer be included
I stumbled over node.js sometime ago and like it a lot. But soon I
I stumbled over a curious bug, I think: I tried to read 512 as
The last week I stumbled over this paper where the authors mention on the
I've recently stumbled over this expression: True == False in (False,) It evaluates to
After playing around with haskell a bit I stumbled over this function: Prelude Data.Maclaurin>

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.