1

for example

An interface of

interface StateInterface { val variationTypes: List<VariationType> get() = emptyList() object EMPTY : StateInterface } 

then its been declared inside an actionbean like this

open val stateInterface: StateInterface = StateInterface.EMPTY

Is it all it does it just create a new interface? Why we need to do it this way?

1 Answer 1

3

You don't need to do it that way.

interface StateInterface { val variationTypes: List<VariationType> get() = emptyList() } object EMPTY : StateInterface 

Would work fine, but the author decided that they wanted the usage to read StateInterface.EMPTY and not just EMPTY.

One advantage or reason for choosing this way is that EMPTY appears in the code completion after typing StateInterface. making it easier to find.

Code Completion

Another readability advantage is that anyone who references StateInterface.EMPTY does not need an additional import line which they would if it wasn't a nested object.

import com.example.StateInterface val x = StateInterface.EMPTY 

This bit open val stateInterface: StateInterface = StateInterface.EMPTY is a property on an object. It's open so descendant implementations can override it. If they do not, StateInterface.EMPTY will be the value of this property.

Sign up to request clarification or add additional context in comments.

1 Comment

And perhaps a third advantage is that you can have another interface with its own EMPTY member, without a clash of names.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.