Hunter
08/26/2025, 7:15 PMinternal
to get the closest behavior but I really would want it to only be accessible in-file. If not, would be very nice to have.Gat Tag
08/26/2025, 7:25 PMAdam S
08/26/2025, 7:58 PMsealed
public type, with a single private
implementation.
sealed class FooService {
abstract val name: String
}
private class FooServiceImpl(
override val name: String
) : FooService()
val defaultFooService: FooService = FooServiceImpl("default")
Zach Klippenstein (he/him) [MOD]
08/26/2025, 8:08 PMZach Klippenstein (he/him) [MOD]
08/26/2025, 8:08 PMkomu
08/27/2025, 5:00 AM@file:OptIn(PrivateToMe::class)
@RequiresOptIn
private annotation class PrivateToMe
@PrivateToMe
fun foo() { }
fun bar() {
foo()
}
That said, this does feel a bit wonky and I haven't actually used it anywhere.
I've also found myself often wanting file private access.Zach Klippenstein (he/him) [MOD]
08/27/2025, 8:17 PMGat Tag
08/27/2025, 8:19 PMCLOVIS
08/28/2025, 7:56 AMYou could have an opt-in annotation that itself is not visible outside the file so no-one else can use it:IMO, if you're going to do this, use
@Deprecation(level = DeprecationLevel.HIDDEN)
so it's not even in the generated documentation, and you don't need a custom annotationAdam S
08/28/2025, 8:28 AMYou could have an opt-in annotation that itself is not visible outside the file so no-one else can use it:Nitpick: anyone could opt-in via compiler args 😅
kotlin {
compilerOptions.optIn.add("PrivateToMe")
}
CLOVIS
08/28/2025, 8:37 AM@Suppress("INVISIBLE_REFERENCE")
if they're going that far 😅Adam S
08/28/2025, 8:42 AM