Constructors


#1

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?


#2

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


#3

It turns out that

;{   code block }

works best.

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


val list: Object = Object()


#4

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