Quantcast
Channel: Kotlin: 'val' on secondary constructor parameter is not allowed - Stack Overflow
Viewing all articles
Browse latest Browse all 5

Kotlin: 'val' on secondary constructor parameter is not allowed

$
0
0

I have following class:

class Person(val name: String) {    private var surname: String = "Unknown"    constructor(name: String, surname: String) : this(name) {        this.surname = surname    }}

But when I want to have the name parameter immutable in second constructor:

constructor(val name: String, surname: String) : this(name) {    this.surname = surname}

I have the following compile-time error:

Kotlin: 'val' on secondary constructor parameter is not allowed

Can someone explain why is Kotlin compiler not allowing to do this?


Viewing all articles
Browse latest Browse all 5

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>