Aladdin - Scala Bugtracking
[#1057] project: compiler priority: low category: bug
submitter assigned to status date submitted
Nikolay Martin won't fix 2007-04-18 13:42:12.0
subject [contrib #431] initialization order problem
code
class Bar extends Ordered[Bar] {
  val id = Bar.add(this) // yields bug; maybe compiler should forbid to use "this" as long as it is not initialized??
  // this works: val id = Bar.nextId; Bar.collec += this
  override def compare(that: Bar) = this.id - that.id
  override def toString() = "" + id
}

object Bar {
  val collec = new scala.collection.jcl.TreeSet[Bar]
  var id = 0
  def nextId: Int = {id = id + 1; id}
  def add(bar: Bar): Int = {collec += bar; nextId}
}

object Foo {
  def main(args: Array[String]) {
    val bars = List(1,2,3,4,5).map(a => new Bar())
    Bar.collec -= bars(2)
    Console.println("" + Bar.collec)
  }
}
what happened
Set(5, 4, 3, 2, 1)
what expected Set(1, 2, 4, 5)
[back to overview]
Changes of this bug report
Nikolay  edited on  2007-04-18 13:46:25.0
I don't quite understand this contribution #431. When I use ListBuffer it prints in the expected order. Maybe jcl.TreeSet prints in reverse order or sth like that. Currently, jcl.TreeSet.toString yields "" so I assign to Sean to clean this mess and decide whether this is really a bug or not.
Sean  edited on  2007-04-22 03:20:22.0
Somebody added a toString method in (or was it always there?) and given linearization...it supercedes the one in most of the JCL classes. So is the fragility of linearization. I'll try to ensure that JCL classes inherit mixins in the right order (if this is possible).
Sean  edited on  2007-04-22 04:10:01.0
There was a linearization problem in the JCL that caused Set's toString method to be used (which is just ). Fixed that. The initialization bug that the contributor really cares about still stands. Re-assigning to martin.
Sean  edited on  2007-04-22 04:48:38.0
On second thought, I think this is a non-issue for Scala at least. Initialization that occurs here is no more fragile than in Java. Safe initialization is pretty much impossible to achieve in an imperative language.