Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does BitSet require an explicit cast to be considered as an instance of Set[Int]?

In the scaladoc, BitSet is defined as extending Set[Int]. So I thought using a BitSet as in instance of Set[Int] would work, but I get a type mismatch:

Welcome to Scala version 2.9.1.final (Java HotSpot(TM) 64-Bit Server VM, Java 1.6.0_29).

scala> import collection.BitSet
import collection.BitSet

scala> val b: Set[Int] = BitSet()
<console>:8: error: type mismatch;
 found   : scala.collection.BitSet
 required: Set[Int]
       val b: Set[Int] = BitSet()
                               ^

However a cast works:

scala> val b: Set[Int] = BitSet().asInstanceOf[Set[Int]]
b: Set[Int] = BitSet()

So why do I need to explicitely cast a BitSet to a Set[Int] while Set[Int] is a super-type of Set[Int]?

like image 825
Julien Gaugaz Avatar asked Mar 13 '12 21:03

Julien Gaugaz


People also ask

What is the use of the set () function in bitset?

bitset::set() is a built-in STL in C++ which sets the bit to a given value at a particular index. If no parameter is passed, it sets all bits to 1. If only a single parameter is passed, it sets the bit at that particular index to 1.

What does bitset mean in C++?

Bitset is a container in C++ Standard Template Library for dealing with data at the bit level. 1. A bitset stores bits (elements with only two possible values: 0 or 1). We can however get the part of a string by providing positions to bitset constructor (Positions are with respect to string position from left to right)

What is bitset in programming?

Bitset represents a fixed-size sequence of N bits and stores values either 0 or 1. Zero means value is false or bit is unset and one means value is true or bit is set. Bitset class emulates space efficient array of boolean values, where each element occupies only one bit.

What is bitset in C++ Mcq?

This set of C++ Programming Multiple Choice Questions & Answers (MCQs) focuses on “Bitset – 1”. 1. What is bitset in C++? Explanation: Bitset is a collection of bool variables with each element consuming only one bit.


1 Answers

It turns out that your Set is actually scala.collection.immutable.Set. So you can

val b0: Set[Int] = collection.immutable.BitSet()
val b1: collection.Set[Int] = collection.BitSet()
val b2: collection.immutable.Set[Int] = collection.immutable.BitSet()
val b3: collection.mutable.Set[Int] = collection.mutable.BitSet()
val b4: collection.Set[Int] = collection.immutable.BitSet()
val b5: collection.Set[Int] = collection.mutable.BitSet()

but not any of

val x1: collection.immutable.Set[Int] = collection.BitSet()
val x2: collection.immutable.Set[Int] = collection.mutable.BitSet()
val x3: collection.mutable.Set[Int] = collection.BitSet()
val x4: collection.mutable.Set[Int] = collection.immutable.BitSet()

and it turns out that the default import for Set gives you x2. Import collection.immutable.BitSet, or import collection.Set (to cover collection.immutable.Set).

like image 190
Rex Kerr Avatar answered Sep 28 '22 08:09

Rex Kerr