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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T18:38:23+00:00 2026-05-20T18:38:23+00:00

I guess, type variance annotations ( + and – ) cannot be applied to

  • 0

I guess, “type variance annotations” (+ and -) cannot be applied to “type members”. In order to explain it to myself I considered the following example

abstract class Box {type T; val element: T}

Now if I want to create class StringBox I have to extend Box:

class StringBox extends Box { type T = String; override val element = ""}

So I can say that Box is naturally covariant in type T. In other words, the classes with type members are covariant in those types.

Does it make sense ?
How would you describe the relationship between type members and type variance ?

  • 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-20T18:38:24+00:00Added an answer on May 20, 2026 at 6:38 pm

    Box is invariant in its type T, but that doesn’t mean there’s nothing to see.

    abstract class Box {
      type T
      def get: T
    }
    type InvariantBox = Box { type T = AnyRef }
    type SortofCovariantBox = Box { type T <: AnyRef }
    

    What alters the variance situation is the degree to which the type is exposed and the manner it is done. Abstract types are more opaque. But you should play with these issues in the repl, it’s quite interesting.

    # get a nightly build, and you need -Ydependent-method-types
    % scala29 -Ydependent-method-types
    
    abstract class Box {
      type T
      def get: T
    }
    type InvariantBox = Box { type T = AnyRef }
    type SortofCovariantBox = Box { type T <: AnyRef }
    
    // what type is inferred for f? why?
    def f(x1: SortofCovariantBox, x2: InvariantBox) = List(x1, x2)
    
    // how about this?
    def g[U](x1: Box { type T <: U}, x2: Box { type T >: U}) = List(x1.get, x2.get)
    

    And etc.

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

Sidebar

Related Questions

I guess I'll illustrate with an example: In this game you are able to
I guess it should be a common technique, However, I tried the following two
I guess, the following is a standard problem on every school or university: It
Can you guess what is the reason to not allow sealed classes for type-constraints
I have to process these mime type string. For avc1.42E00C, I guess 'avc1' means
I guess the question is really about how to define a type as an
I'm parsing Java source files and I need to extract type information to guess
I guess this could also be asked as to how long the created type
I'm having a problem getting this type conversion working correctly. My guess is the
An easy question I guess, but in the documentation of the Type class they

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.