Cannot return in init?


#1
class HelloWorld(int: Int?) {
    init {
        if(int == null) 
            return       // 'return' is not allowed here
    }
}

class HelloWorld {
    constructor(int: Int?) {
        if(int == null)
            return        // This is fine
    }
}

Is there a way to return in init? or I am forced to use constructor?


#2

Alternatively you can write it so that you don’t need the return:

class HelloWorld(int: Int?) {
    init {
        int?.let {
            // Do something with int
        }
    }
}