Scala BitSet implemented with Java BitSet, for use in Scala Enumerations to replace ValueSet Due to a performance profiling hotspot detailed here, I implemented my own BitSet using Java's BitSet.  This is intended to replace the Enumeration.ValueSet.  However, it's a bit awkward to use, primarily due to my likely misunderstanding of the relationships between the Enumeration class, Enumeration type and concrete Enumeration object.

In my enumeration objects, I have to have code like this:

type BitSet = alder.BitSet[this.type]
val empty = alder.BitSet[this.type]()


Elsewhere, I need to do things like this:

alder.BitSet.fromBitMask[SomeEnumeration.type](...)


For mkString I need to actually pass in the enumeration object itself.  Is there any way to make this entire edifice a little more user-friendly?

package alder

import scala.language.implicitConversions

class BitSet[E &lt;: Enumeration](val bits: java.util.BitSet) {

  def isEmpty: Boolean = bits.isEmpty

  def nonEmpty: Boolean = !isEmpty

  override def hashCode = bits.hashCode

  override def equals(o: Any) = o match {
    case that: BitSet[E] =&gt; this.bits equals that.bits
    case _ =&gt; false
  }

  def union(that: BitSet[E]): BitSet[E] = {
    var newBits = bits
    newBits.or(that.bits)
    new BitSet[E](newBits)
  }

  def |(that: BitSet[E]): BitSet[E] = union(that)

  def |(v: E#Value): BitSet[E] = union(BitSet(v))

  def intersection(that: BitSet[E]): BitSet[E] = {
    var newBits = bits
    newBits.and(that.bits)
    new BitSet(newBits)
  }

  def &amp;(that: BitSet[E]): BitSet[E] = intersection(that)

  def &amp;(v: E#Value): BitSet[E] = intersection(BitSet(v))

  def contains(v: E#Value): Boolean = bits.get(v.id)

  def containsAll(that: BitSet[E]): Boolean = intersection(that) == that

  def containsAny(that: BitSet[E]): Boolean = intersection(that).nonEmpty

  def toBitMask(): Array[Long] = bits.toLongArray

  def mkString(e: E, sep: String): String =
  {
    val vs = e.ValueSet.fromBitMask(toBitMask())
    vs.mkString(sep)
  }

}

object BitSet {

  def apply[E &lt;: Enumeration](): BitSet[E] = new BitSet[E](new java.util.BitSet)

  def apply[E &lt;: Enumeration](vs: E#Value*): BitSet[E] = {
    var bits = new java.util.BitSet
    for (v &lt;- vs) {
      bits.set(v.id)
    }
    new BitSet[E](bits)
  }

  def fromBitMask[E &lt;: Enumeration](mask: Array[Long]): BitSet[E] =
    new BitSet[E](java.util.BitSet.valueOf(mask))

}
After studying your code for a good long while, I realized that the reason you haven't gotten an answer is that there isn't really much to say about your code. Well done!

I would recommend adding some thorough ScalaDoc though. It has been my experience that Scala library developers have far too much faith in the ability of library users to magically understand what they really meant. Examples are wonderful things.