1
我认为问题标题有点混乱,但我找不到更准确的说法。
我只需要一个简单的代码示例来告诉你我想要什么。我们可以代表Java(或Kotlin)中的“自我类”吗?
我:
// code 1
interface A { A bla(); }
class B implements A { @Override public B bla() { return this; } }
class C implements A { @Override public C bla() { return this; } }
但实际上,这段代码编译太(DIFF:看的返回类型声明):
// code 2
interface A { A bla(); }
class B implements A { @Override public A bla() { return this; } }
class C implements A { @Override public A bla() { return this; } }
我想代码2是一个类型的错误。假设我想强制每个A
的子类'bla
方法返回自己,而不是A
。
我觉得有可能是假的代码代表什么,我想:
interface A { this.Type bla(); }
就像Haskell的类型类:
class Monad (m :: * -> *) where
-- here m is restricted to the subclass
(>>=) :: m a -> (a -> m b) -> m b
这可能吗?
不可能在Java中,不知道科特林。 – Oleg
噢不,坏消息TAT @Oleg – ice1000
@ ice1000我已经使用过这个库,我想这是你要找的:https://github.com/h0tk3y/kotlin-monads;) –