file-private should have its own keyword so we can...
# language-proposals
m
file-private should have its own keyword so we can designate class members as file-private
Copy code
package a

class B {
  // only accessible from within this file
  file-private val priv = 123
}

fun c(b: B) {
  println("I can access ${b.priv}")
}
12
👍 4
👍🏼 1
This allows file private to be more of a replacement for java’s package-private
j
STRONG agree. Love this from Swift.
Note that Swift uses
fileprivate
, and visually I like it better due to lack of hyphen. Also not sure if the hyphen would introducer parser ambiguities or not.
👍🏻 1
👍 3
l
No ambiguity in a class/object declaration since you can't use subtraction there. Visibility modifiers are already not supported in function bodies, where subtraction can happen.
j
Sorry it's the lexer not the parser
m
fileprivate
also follows existing compound keyword convention such as
typealias
☝️ 4
b
What about something like labeled visibility modifiers, like
private@file
? Extend this notation to target specific types to also be visible from (similar to friend classes in C++)
Copy code
class A {
    private@AFriend val something: String = "something"
}

class AFriend {
    fun getSomething(a: A): String = a.something
}
And if Kotlin ever gets union types:
Copy code
internal typealias Alphabet = A | B | C
// use private@Alphabet
👍🏼 1
👍 2
(of course, you could just have all the 'friend' classes/members in the same file and a
fileprivate
keyword would be plenty)
k
If we were to have
fileprivate
, should we also have
filesealed
?
j
No? How is that different than
fileprivate sealed class
?
k
I think
fileprivate sealed class
would make the class only accessible to other code in the same file. On the other hand,
filesealed
would make the class only extendable by other classes in the same file, but the class would be visible outside of that file. That would be the equivalent of Java's sealed classes with the
permits
clause naming only the classes in the same file.
j
Seems like an orthogonal concern to visibility then. And I would prefer the
permits
approach rather than conflating it with visibility / location.
b
Couldn't you just give the
sealed
class a
fileprivate
constructor?
👌 4