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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T22:19:52+00:00 2026-06-03T22:19:52+00:00

I believe one can define covariance (at least, for objects) as ‘the ability to

  • 0

I believe one can define covariance (at least, for objects) as ‘the ability to use a value of a narrower (sub) type in place of a value of some wider (super) type’, and that contravariance is the exact opposite of this.

Apparently, Scala functions are instances of Function[-A1,…,+B] for contravariant parameter types A1, etc. and covariant return type, B. While this is handy for subtyping on Functions, shouldn’t the above definition mean I can pass any supertypes as parameters?

Please advise where I’m mistaken.

  • 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-03T22:19:54+00:00Added an answer on June 3, 2026 at 10:19 pm

    Covariance and contravariance are qualities of the class not qualities of the parameters. (They are qualities that depend on the parameters, but they make statements about the class.)

    So, Function1[-A,+B] means that a function that takes superclasses of A can be viewed as a subclass of the original function.

    Let’s see this in practice:

    class A
    class B extends A
    val printB: B => Unit = { b => println("Blah blah") }
    val printA: A => Unit = { a => println("Blah blah blah") }
    

    Now suppose you require a function that knows how to print a B:

    def needsB(f: B => Unit, b: B) = f(b)
    

    You could pass in printB. But you could also pass in printA, since it also knows how to print Bs (and more!), just as if A => Unit was a subclass of B => Unit. This is exactly what contravariance means. It doesn’t mean you can pass Option[Double] into printB and get anything but a compile-time error!

    (Covariance is the other case: M[B] <: M[A] if B <: A.)

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

Sidebar

Related Questions

I can't believe all I find to define role hierarchies is some strange .xml
I have the following two queries, I believe that the one that uses the
I came with below solution but I believe that must be nicer one out
Alright, I'm having some issues and I believe it's a CSS one. Here is
Premise I believe that there is a way to objectively define "Good" and "Bad"
I'm working on a project and have a problem that I believe can be
This situation can only occur without name mangling (I believe), so the below code
I believe I read at some point that due to Android running on the
I believe I understand properties for the most part. My question is, if I
I believe the answer is no . And am looking for a counter example

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.