Delegates must be constructor arguments?


#1

It would appear so. Do you think it would be good to allow normal instance variables too?

class Pager(elt: Element) : Element by elt2 {   // by elt is ok, but not by elt2
  private val elt2: Element = buildDiv()
}

Rob


#2

There are issues with allowing that: for example, that property might not have been initialized by the time someone calls a delegated method (from an initializer of another property, for example)


#3

That makes sense.  A related question: should this not compile?

class Bar {
  var s: String


  {
  helper()
  s = “”
  }


  private fun helper() {
  println(s.length)  
  }
}

It compiles without warning and throws a NullPointerException.

Rob


#4

It would be nice to prohibit things like this (and Ceylon does, for example), but restrictions it imposes are rather severe, so we decided not to make this checks