Semyon Danilov
Semyon Danilov

Reputation: 1773

Property accessibility in Kotlin

Here is what we know from the docs: getter of public property can't be private (seems logical enough), so:

@Inject
var repository: MyExampleRepository? = null
    private get

won't compile. Ok, so maybe we can make property private and define setter public?

@Inject
private var repository: MyExampleRepository? = null
    public set

This will compile and value will actually be injected, but I still can't use this in code, so:

service.repository = null

gives compilation error:

Kotlin: Cannot access 'repository': it is 'private' in 'MyService'

I wonder if it is possible to have private property with public setter.

Upvotes: 3

Views: 459

Answers (1)

miensol
miensol

Reputation: 41638

It's a known issue: KT-10385:

Kotlin doesn't generate setter method for the following code:

private val someProperty: Integer
public set

The intention is to generate a set only property. Use case including spring dependency injection.

Upvotes: 2

Related Questions