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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:57:38+00:00 2026-06-06T13:57:38+00:00

I have a trait and an implementation looking like: trait Foo[A] { def bar[B

  • 0

I have a trait and an implementation looking like:

trait Foo[A] {
  def bar[B >: A: Ordering]: Foo[B]
}
class FooImpl[A]( val a: A, val values: List[Foo[A]] ) extends Foo[A] {
  def bar[B >: A] = { /* concrete implementation */}
}  

I would like to use the @specialized annotation on A and B to avoid autoboxing. Do I need to use it in both trait and implementation, only in implementation, or only in trait ?

  • 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-06-06T13:57:41+00:00Added an answer on June 6, 2026 at 1:57 pm

    The REPL has the right answer for us, together with javap, which will show the disassembled java code. If you add tools.jar to your REPL classpath, you will be able to do cool things like the following:

    scala>  trait Foo[@specialized(Int) A] { def doSomething(a:A)}
    defined trait Foo
    
    scala>  :javap -p Foo
    Compiled from "<console>"
    public interface Foo{
        public abstract void doSomething(java.lang.Object);
        public abstract void doSomething$mcI$sp(int);
    }
    
    scala> class Hello extends Foo[Int] { def doSomething(a:Int)=println(a)}
    defined class Hello
    
    scala>  :javap -p Hello
    Compiled from "<console>"
    public class Hello extends java.lang.Object implements Foo$mcI$sp,scala.ScalaObject{
        public void doSomething(int);
        public void doSomething$mcI$sp(int);
        public void doSomething(java.lang.Object);
        public Hello();
    }
    

    So now it should be clear to you that providing the @specialized only at trait level is enough: in the Foo interface you clearly have two method declaration. It looks to me that a trick is going on there, however:

    scala>  new Hello
    res0: Hello = Hello@7a80747
    
    scala>  res0.doSomething("test")
    <console>:11: error: type mismatch;
     found   : java.lang.String("test")
     required: Int
    

    While I can answer your question, there are some questions which I can’t answer:

    • Why the methods are defined as public abstract in the trait?
    • Why the method doSomething(java.lang.Object) is there in the disassembled class, but cannot be called?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Why if I have: trait T { def method(a: Int) } class A extends
I have the following code implementation of Breadth-First search. trait State{ def successors:Seq[State] def
I have an implementation for printing out enum values in c++ If I put
I would like to have a sealed trait which have a declared method that
Suppose I have the following abstract class Strategy { val lib: TestingLibrary = ...
Suppose I have the following trait trait Market { def getCurrency: String } With
I'd like to define a trait with some properties which have a well defined
I have the following classes/traits in Scala trait Write[-T] { def add(elem : T);
I was under the impression that this // short syntax def foo(bar: Bar)(baz: Baz):
Say I have a number of trait s that each add some behaviour to

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.