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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T02:59:55+00:00 2026-06-17T02:59:55+00:00

I have code: class A { override def toString = object class A }

  • 0

I have code:

class A {
    override def toString = "object class A"
}

class B extends A {
    override def toString = "object class B"
}

class Cell[+T](init: T) {
    private[this] var current: T = init
    def get: T = current
    def set(x: T) { current = x }
}

val cB = new Cell[B](new B)
println(cB.get)
val cA: Cell[A] = cB
println(cA.get)

but I have error in line: def set(x: T) { current = x }

error: covariant type T occurs in contravariant position in type T of
value x
def set(x: T) { current = x }

Explain, please

  • 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-17T02:59:56+00:00Added an answer on June 17, 2026 at 2:59 am

    Contravariant positions for a type are (among others) any positions that allow you to pass an instance of that type to a method. So all method parameter types are in contravariant positions. Since you declared T as covariant ( +T), the compiler won’t allow this. Your only options are:

    • make T invariant
    • modify the set method such that it returns a new instance of Cell and Cell thus becomes immutable.
    • remove the set method, also making Cell immutable

    If the compiler allowed you to have a set method as you implemented it, that would make the type system unsafe, since it would allow you to write:

    val cs:Cell[String] = new Cell("")
    val ca:Cell[Any] = cs
    ca.set(5)
    val s:String = cs.get //boom
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the next code : public class AddPrinter extends ListActivity { @Override public
I have this code: class SomeClass: @classmethod def func1(cls,arg1): #---Do Something--- @classmethod def func2(cls,arg1):
Have a look at the following code: class A(object): defaults = {'a': 1} def
Say I have code like this: abstract class Animal[T <: Animal[T]] { def mateWith(that:
I have code: class Scene def initialize(number) @number = number end attr_reader :number end
I have this code: class LFSeq: # lazy infinite sequence with new elements from
I have this code: class MyObj { int Id; string Name; string Location; }
I have the following code: class Parameterizable{ var map: Map[String, String] = new scala.collection.immutable.HashMap()
class DefaultListMap[A, B <: List[B]] extends HashMap[A, B] { override def default(key: A) =
I'm converting some old Java code to Scala. I have this. class XYCustomRenderer(dataMax: Double,

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.