Delegates must be constructor arguments?

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

1 Like

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)

1 Like

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

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

Some class types need zero argument constructors, such as Android Fragments. How would one attache a delegate in such cases?

1 Like