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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:27:49+00:00 2026-05-27T23:27:49+00:00

In Scala a Set is a function: trait Set[A] extends (A => Boolean) This

  • 0

In Scala a Set is a function:

trait Set[A] extends (A => Boolean)

This make it impossible to have a covariant immutable Set because type A occurs in contravariant position. In contrast Seq is not defined as a function. There is already some content about the question why Sets and Seqs are designed this way:

  • Why is Scala’s immutable Set not covariant in its type?
  • Scala: Why does Seq.contains take an Any argument, instead of an argument of the sequence type?
  • Why does Seq.contains accept type Any rather than the type parameter A?

One answer says that the reason for this is the mathematical background. But this answer wasn’t explained a little more. So, what are the concrete advantages to define a Set as a function or what would be the disadvantages if it is implemented differently?

  • 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-27T23:27:50+00:00Added an answer on May 27, 2026 at 11:27 pm

    The set of type Set[A] has to have a method that tests if an element of type A is in the set. This method (apply) has to have a parameter of type A representing that element, and that parameter is in the contravariant position. This means that sets cannot be covariant in their type parameter A. So – it’s not the extending of the function interface that makes it impossible to have covariant immutable sets, it’s the existence of the contravariant apply method.

    And for reasons of convenience, it makes sense to extend the Function1 interface to be able to pass sets around and treat them as functions.

    By contrast, sequence abstraction doesn’t have a method that tests if an element is in the sequence, it only has the indexing method – apply takes an integer index, and returns the element at that index. Sequences are also defined as functions, but functions of type Int => A (which are covariant in A), not A => Boolean, as sets.

    If you want to know more about how type safety would be broken if sets were defined as covariant in their type parameter A, see this example in which the set implementation does some writing to private members for reasons of caching the lookups (below @uV is the annotation which disables variance checking and expensiveLookup is meant to simulate a call to a computationally expensive check if an element is in the set):

    import annotation.unchecked.{uncheckedVariance => uV}
    
    trait Set[+A] {
      def apply(elem: A @uV): Boolean
    }
    
    class CachingSet[+A >: Null] extends Set[A] {
      private var lastLookup: (A @uV, Boolean) = (null, false)
      private def expensiveLookup(elem: A @uV) = (elem, true)
      def apply(elem: A @uV): Boolean = {
        if (elem != lastLookup._1) lastLookup = expensiveLookup(elem)
        lastLookup._2
      }
      def lastQueriedElement: A = lastLookup._1
    }
    
    object Main extends App {
      val css = new CachingSet[String]
      val csa: CachingSet[AnyRef] = css
    
      csa.apply(new AnyRef)
      val s: String = css.lastQueriedElement // you'll get a ClassCastException here
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In Scala I have the following trait: trait Reactive { type Condition = ()
Scala programmer should have known that this sort of writing : class Person{ var
Say I have set up the following function f[a,b,c] which I wish to plot
I have this segment of Scala code which defines an ordering and applies it
Can I set maximum size for an actor's mailbox in Scala? Take the Producer-Consumer
scala> val m = Map(1 -> 2) m: scala.collection.immutable.Map[Int,Int] = Map(1 -> 2) scala>
In Scala, is it possible to get the string representation of a type at
I'm trying to make a binary tree in scala and need to make a
Using a __set accessor function in PHP I can set the value of a
Regarding to SQL performance. I have a scalar valued function for checking some specific

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.