Problem: Scope in primary constructor is not class scope


#1

I am now experimenting with kotlin and I have noticed  an inconsistency between primary and secondary constructor that seems to me quite inconvenient. I will explain it with an example. Suppose that I define a class with a primary constructor that have some default parameters, and suppose that I define the value of this default parameters in a companion object

class MyClassWithLongName(val param1:Int =MyClassWithLongName.MMM*MyClassWithLongName.NNN){
    companion object {
        val NNN:Int=12
        val MMM:Int=13
    }
}

I can't leave out the scope of MMM and NNN in the primary constructor and simply write

class MyClassWithLongName(val param1:Int =MMM*NNN){
    companion object {
        val NNN:Int=12
        val MMM:Int=13
    }
}

But I can do it if do it in a secondary constructor

class MyClassWithLongName{
    private var param1: Int
    constructor(param1:Int =MMM*NNN)  {
        this.param1=param1;    
    }
    companion object {
        val NNN:Int=12
        val MMM:Int=13
    }
}

But I what I gain here in conciseness I loose because I have to declare explicitely the property param1 and the constructor itself.

A similar problem happens for a different use-case.
Suppose that I define some nested classes and I use objects of these nested classes in the primary constructor.
I have to write

class MyClassWithLongName(val aInstance:MyClassWithLongName.A,val bInstance:MyClassWithLongName.B){
  class A
  class B
}

but I would much prefer to simply write

class MyClassWithLongName(val aInstance:A,val bInstance:B){
    class A
    class B
}

Why Kotlin could not just use the class scope in the primary constructor?


#2

> Why Kotlin could not just use the class scope in the primary constructor?

Answering your question literally: because class members are not available before the super-constuctor is called.

Answering your issues: we  are considering fixing them.