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

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

The Archive Base Latest Questions

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

This a very simple scala sample, but it can’t be compiled: abstract class Box[+A]

  • 0

This a very simple scala sample, but it can’t be compiled:

abstract class Box[+A] {
    var value: A = _
}

The error is:

covariant type A occurs in contravariant position in type A of parameter of setter value_=

What I want the class to do is:

class StringBox extends Box[String]
class DateBox extends Box[Date]

object Testbox {
    def main(args: Array[String]) {
        val list = ListBuffer[Box[Any]]()
        val str = new StringBox
        str.value = "abc"
        val date = new DateBox
        date.value = new Date
        list += str
        list += date
        println(list)
    }
}
  • 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-20T16:48:54+00:00Added an answer on May 20, 2026 at 4:48 pm

    It does not compile because it is incorrect. If scala were to allow you to do it, then you could violate type safety. For example:

    import scala.annotation.unchecked.uncheckedVariance
    
    abstract class Box[+A] {
        var value: A  @uncheckedVariance = _
    }
    
    class StringBox extends Box[String]
    val sb = new StringBox; sb.value = "abc"
    val sa: Box[Any] = sb
    sa.value = 5
    println(sb.value.length)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sure this is a very simple question, but embarrassed to say I can't
Have a look at this very simple example WPF program: <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml
i am trying to compile this very simple piece of code class myList {
This is a very simple question with a simple answer, but it is not
I am sure this is a very simple problem, but I am new to
This must be a very simple question, but I don't seem to be able
This may be a very simple problem, but I couldn't find an answer googleing
I'm sure this is a very stupid question but I can't find the answer,
there is probably a very simple answer to this very simple question, but i
This very simple code gives me tons of errors: #include <iostream> #include <string> int

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.