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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T16:49:25+00:00 2026-05-16T16:49:25+00:00

In Scala variance can be defined with variance operators like + and – on

  • 0

In Scala variance can be defined with variance operators like + and – on the generic type argument. For example the List type is covariant in the standard library.

class List[+A]

So a function with a covariant list can be defined like this:

def foo[A](list : List[A])

Also variance can be emulated with generic bounds. So we can also write this

def foo[A](list : List[_:< A])

of course this makes no sense, because list is already covariant. But the same trick could be done for types that are not covariant. (like Stack). Of course, also a new types could be created from stack (inheritance of aggregation) that is covariant.

So my questions:

  1. When should be use generic bounds for variance? And when should we create a new covariant type?
  2. Are generic bounds only useful for variance, or can they declare more (language concepts).
  3. If they are only useful for variance, are bounds then only for compatibility with Java?

thx in advance 🙂

  • 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-16T16:49:26+00:00Added an answer on May 16, 2026 at 4:49 pm

    If a type is naturally covariant or contravariant you should declare it so. Your users will thank you for it. The use-site variance is indeed mostly there because of Java. More precisely, a type such as Array[T <: Number] is treated as a shorthand for an existential type:

    ArrayBuffer[T] forSome { type T <: Number }
    

    Existential types have a pretty bulky syntax in Scala. That’s sort of intentional, because we do not recommend you use them much. When would you need an existential type?

    1. To write the analogue of a Java type with wildcards, such as List<? extends Number>.
    2. To write the analogue of a Java raw type, such as List.

    In Java, raw types and wildcard types are not quite the same and neither is quite the same as an existential type (even though we know what they are not, it’s pretty hard to state precisely what they are). But they are close enough to existentials in practice, so that Scala gets away with mapping them to this kind of type.

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

Sidebar

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.