Constructors

Hello,

with Kotlin M6, when the logic for initializing class members was a bit complicated, I was using a “constructor function” like this:

public class MyClass(arg: String) {
  public val m1: String
  MyClass() {
  m1 = computeM1(arg)
  }
}

I am trying to upgrade to M7, but this doesn't work anymore. The compiler complains about MyClass not being an annotation class on the line: MyClass() {

I could use pretty much any name, it didn’t have to be MyClass(). I tried using another name, but then it still tries to resolve the name into an annotation class.

Did the syntax for those “constructor function” change?

It seems to compile if I just replace MyClass() { with simply {. Is it going to be executed as a constructor function as before?

It turns out that

;{   code block }

works best.

The ; is required if the line before the block is something like


val list: Object = Object()

What you were using was not a "constructor function", just an annotated anonymous initializer. You are right: removing the annotation is the correct fix