Kotlin是否允许一个函数返回这个接口的实现类型的值?

我想写这样的东西:

/** * Represents a state, large or small. */ interface State { /** * */ val changeValue: StateChange<This> // does not compile; "This" is a keyword I made up that might reference the implementing class } /** * Represents a change in a state, large or small. * * @param StateType The type of state that has changed */ interface StateChange<StateType: State> 

但是,正如我的评论所述,我不知道这个语法。 那是存在的吗? 或者,我是否必须使用强制国家作为一个通用参数的解决方法?


如果它不是明显的,我不能使用val changeValue: StateChange<State>因为那么这使得这成为可能,我不希望:

 // BAD EXAMPLE - This is what I want to avoid class Foo: State { val changeValue: StateChange<Bar> = Bar().changeValue } class Bar: State { val changeValue: StateChange<Foo> = Foo().changeValue } 

你可以使用一个递归的上限为这个类:

 interface State<T : State<T>> { val changeValue: StateChange<T> } interface StateChange<StateType: State<StateType>> 

现在这个代码将不再编译:

 class Foo: State<Foo> { val changeValue: StateChange<Bar> = Bar().changeValue } class Bar: State<Bar> { val changeValue: StateChange<Foo> = Foo().changeValue }