CLOVIS
10/28/2026, 10:16 AMclass Foo private constructor( … ) {
}
This constructor would be way more useful if it was file-private instead of class-private
Because of this I'm often forced to have internal constructor , which means the constructor is public for Java consumers 😕Marat Akhin
10/28/2026, 10:17 AMCLOVIS
10/28/2026, 10:21 AMyou worry about the Java consumers mostly?I do worry about Java consumers, but I wouldn't say 'mostly'. I often need "Some type that can only be instantiated through utility functions". • If I make the constructor
private, then it's not available to utility functions
• If I make the constructor internal, then I give myself the chance to misuse it in the entire module
• …and also to Java consumersCLOVIS
10/28/2026, 10:23 AMprivate constructor of a top-level class would be callable in the file the class is defined in, as that's still very localized so there's less chance of accidental misuse than with internalCLOVIS
10/28/2026, 10:26 AMYoussef Shoaib [MOD]
10/28/2026, 10:26 AMprivate change meanings would probably be too much IMO.
I sometimes want truly class-private thingsChrimaeon
10/28/2026, 10:26 AMI very often have public classes in my API that users shouldn't be able to instantiate directlyIn a library you should anyway share interfaces and not classes
Youssef Shoaib [MOD]
10/28/2026, 10:28 AMIn a library you should anyway share interfaces and not classesI (respectfully) disagree. A library might have `value class`es, for instance, that it exposes for the user's convenience, and then hiding the constructor is extremely important
CLOVIS
10/28/2026, 10:30 AMByteVector, FloatVector and BooleanVector have `internal`constructors just because they're called in Vector.fromBinaryData (same file), which is the only function that makes the check to decide whether they are valid. Calling either of these constructors from anywhere else in the codebase is almost certainly incorrectCLOVIS
10/28/2026, 10:32 AMIn a library you should anyway share interfaces and not classesFirst,
sealed interface, because regular interfaces are way worse than an accidentically-callable constructor, and I dare you to find a large library that only contains sealed interfaces in the public APICLOVIS
10/28/2026, 10:36 AMI agree with the use case, but havingchange meanings would probably be too much IMO.private
I sometimes want truly class-private thingsI get that, but also the way
private works is confusing.
private class Foo public constructor()
class Foo private constructor()
The second one is more strict than the first one, even though the first one supposedly says the entire class is private.
To be explicit: I only advocate for this change to apply to constructors, not to any other members. Constructors already behave like top-level functions in many ways.
Said otherwise:
• With this feature, you may expose some things as private-in-file where you really mean private-in-class
• Without this feature, we routinely expose some things as internal (to Kotlin consumers) and public (to Java consumers) when we mean private-in-file
I think the tradeoff points to this feature causing fewer accidental leaksCLOVIS
10/28/2026, 10:42 AMinternal constructor that shouldn't be internal.
However, as an application developer, modules are often way too large, and large teams make it likely that someone will call it at some pointjw
10/28/2026, 11:56 AMfileprivate. Package-only scope and file-only visibility modifiers are sorely missing.ephemient
10/28/2026, 2:33 PMjw
10/28/2026, 2:37 PM