



To my mind, I like the simplicity of saying “The default is always public” rather than “It’s public in this case and that case, but private in this other case”.Īs I say, Scala has public by default and it works very nicely in Scala. What about properties and methods in traits … the only sensible default is ‘public’ by default. So do you have different rules for constructor-parameters-as-properties than for normal properties? Do you force users to write ‘public’ in front of all their constructor-properties? Should these properties be private by default? It’s kind of hard to argue they should for a data class like this there would be no point to the class if they are private. In particular, consider this simple data class.ĭata class Person(val firstname: String, val surname: String, val age: Int) However, Scala choose public as default, and I think that makes sense for the kind of features it has, and for Kotlin too. I think having private the default isn't a bad choice, certainly loads better than module-internal, and for C# private makes sense. Although it’s perhaps not surprising, as I also write Java for a living, and I’ve only seen it (deliberately) used a handful of times for methods or fields. Indeed, on more than one occassion I’ve come accross professional Java developers that did not know that the default access level in Java is ‘package private’. In my experience, ‘package private’ is somewhat common in Java for class definitions (although much less common than ‘public’) but virtually unheard of for methods or fields. I always thought the design in Java of having things be package private by default was a bad choice, and to my mind the obvious default in a language that has properties is ‘public’. Does this include the idea of having things be ‘internal’ access by default? Which says that the modules concept is being redesigned. I’ve read somewhere that the plan for access modifiers in Kotlin (public, private, etc.) is that the default access modifier will be ‘internal to the current compilation module’.
