Anyone know how to fix `Cannot access 'clazz': it ...
# announcements
m
Anyone know how to fix
Cannot access 'clazz': it is private/*private to this*/ in 'Test'
?
Copy code
class Test<out T>(private val clazz: Class<@UnsafeVariance T>) {
    companion object {
        fun test(test: Test<*>) = test.clazz
    }
}
Making
clazz
public or making
T
invariant fixes the error but I'd prefer to avoid that. Companion objects should have access to private members so this should work as is.
y
You can change it to protected if that helps. I'm thinking you might be able to use a private constructor that acts as a public constructor.
m
@yaakov Well, better than public 🙂 I'd still prefer it to be private but protected will work if it that isn't possible. How would a private constructor help here? The error is caused by the companion object method accessing
clazz
. I don't see how changing the constructor visibility would affect that.
y
@mwerschy I think you're right. I tried this and I get the same error. I'm just throwing paint against a wall.
Copy code
class Test<out T> private constructor(val clazz: Class<@UnsafeVariance T>) {
    companion object {
        fun test(test: Test<*>) = test.clazz
    }
}
m
Alright, thanks. I'll go with protected for now.
👍 1