I wish the visibility of a constructor was based o...
# language-evolution
c
I wish the visibility of a constructor was based on the outer scope…
Copy code
class 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 😕
➕ 3
m
Could you share your specific use-case in a bit more detail? Ah, I see, you worry about the Java consumers mostly?
c
I very often have public classes in my API that users shouldn't be able to instantiate directly (either because of additional validation, because there's a factory that may or may not return this specific implementation, or because instantiation should be done through another type).
you 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 consumers
thank you color 1
I would find natural that a
private 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
internal
y
I agree with the use case, but having
private
change meanings would probably be too much IMO. I sometimes want truly class-private things
c
I very often have public classes in my API that users shouldn't be able to instantiate directly
In a library you should anyway share interfaces and not classes
➖ 1
y
In a library you should anyway share interfaces and not classes
I (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
c
Real world example: gitlab.com/opensavvy/ktmongo/-/blob/…/Vector.kt#…
ByteVector
,
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 incorrect
In a library you should anyway share interfaces and not classes
First,
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 API
I agree with the use case, but having
private
change meanings would probably be too much IMO.
I sometimes want truly class-private things
I get that, but also the way
private
works is confusing.
Copy code
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 leaks
Note that this isn't just a library developer issue. As a library author, I use tons of modules and I trust myself to not accidentally use an
internal 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 point
j
In Swift you'd mark it
fileprivate
. Package-only scope and file-only visibility modifiers are sorely missing.
➕ 7
e
I can imagine fileprivate on jvm being implemented by nestmates maybe, but on older java?
j
Package + synthetic bit