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

Answer by flamewave000 for Kotlin: 'val' on secondary constructor parameter is not allowed

$
0
0

The currently accepted answer is correct in explaining why your initial attempt did not work. As such, given your particular scenario, I would inverse the solution and make your secondary constructor the primary, and make that second parameter have a default value.

data class Person(val name: String, val surname: String = "Unknown")

Also, if the class's purpose is to simply hold data, I would make it a data class to improve its handling.


Viewing all articles
Browse latest Browse all 5

Trending Articles



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