Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are all of the base immutable collections final or sealed in scala?

I wrote a trait for sorting iterables in Scala, and it took almost as long to make a class that I could mix it in with as it took to write the trait. Why was the design decision made to disallow users from writing things like:

new List[Int] with SuperAwesomeTrait[Int]?

Now if I want to do this, I need to do some weird hack like,

class StupidList extends LinearSeq {
  val inner = List()
  /* reimplement list methods by calling into inner */
} 

and then

new StupidList[Int] with SuperAwesomeTrait[Int].
like image 429
nnythm Avatar asked Jan 20 '12 21:01

nnythm


1 Answers

Because if I write

someList match {
  case Cons(head, tail) => whatever
  case Nil => somethingElse
}

I don't want my logic broken by a new, unexpected subclass.

I suspect you're trying to solve a problem using subclassing that would be better solved with implicits.

like image 51
James Iry Avatar answered Dec 28 '22 04:12

James Iry