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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:10:59+00:00 2026-05-29T07:10:59+00:00

In my application I want to use such a trait: trait HasBuffer[+A] { var

  • 0

In my application I want to use such a trait:

trait HasBuffer[+A] {

    var items = Buffer[A]()

    def add[A](item: A) { items += item }
    def remove[A](item: A) { items -= item }
    def set(is: Buffer[A]) { items = is }
    def clear { items clear }
}

Classes that inherit this trait should be able to buffer any instances of classes who are children of the class A. However on both the add and remove methods the compiler complains about the item being added or removed from the items that “type mismatch; found : item.type (with underlying type A) required: A”. How should I understand this? What is my mistake here and what to do?

  • 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-29T07:11:01+00:00Added an answer on May 29, 2026 at 7:11 am

    You are parameterizing the methods with another type parameter A that differs from the one of your class definition. Here’s the version you wrote with renamed parameters:

    trait HasBuffer[+A] {
    
        var items = Buffer[A]()
    
        def add[B](item: B) = items += item
        def remove[B](item: B) { items -= item }
        def set(is: Buffer[A]) { items = is }
        def clear { items clear }
    }
    

    Now it should be clear, why the compiler rejects this.

    Instead you can simply write the methods like this:

    def add(item: A) = items += item
    def remove(item: A) { items -= item }
    

    However, then you will still receive compiler errors stating that covariant type A occurs in contra- and invariant positions.

    The point of being covariant is that if you expect a HasBuffer[AnyVal] one may pass in a HasBuffer[Int]. However, if you expect AnyVal and use that type for the add method as well, you would be able to add a completely different type to your HasBuffer[Int]. Hence, the compiler rejects this.

    Instead, you will have to provide a lower bound on the type parameter like this:

    trait HasBuffer[+A, V <: A] {
    
        var items  = Buffer[V]()
    
        def add(item: V) = items += item
        def remove(item: V) { items -= item }
        def set(is: Buffer[V]) { items = is }
        def clear { items clear }
    }
    

    With this you may now have methods like the following:

    def doSomething[X <: AnyVal](b : HasBuffer[AnyVal, X], e : X) = b.add(e)
    

    This method will work on all sorts of HasBuffer type parameter combinations that satisfy the required lower bound.

    Mentally compare this with the idea of not providing a lower bound. Then the method would have become something like this:

    // doesn't make much sense!
    def doSomething(b : HasBuffer[AnyVal], e : AnyVal) = b.add(e)
    

    If you call this with an object of type HasBuffer[Int] and a Double it’ll be all happy. You probably won’t be happy lateron though, when you find your buffer that should contain only Ints now contains a Double.

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

Sidebar

Related Questions

I want to write a web application and want to use Ruby. I have
In a Android application I want to use Scanner class to read a list
In my Rails application I want to use the will_paginate gem to paginate on
I'm developing a web application I want to use the role authentication to control
In my Asp.net MVC 1.0 application I want to use CKEditor as my Rich
I'm trying to get tags working in my rails application and want to use
I want use JQuery mobile for the front-end of my mobile application, but I
I have a .NET application that I want to use as a client to
I have a non-rails application that I want to use rails active-record migrations with.
I am developing an ASP.NET MVC application and I want to use OpenId. What

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.