我正在尝试实现一个IFunctor接口[Haskell的Fuctor类]和一个Maybe创建两个类的接口:Just和Nothing.
到目前为止,我有:
public interface IFunctor<A> {
IFunctor<B> fmap<B> (Func<A,B> f);
}
public interface Maybe<A> : IFunctor<A> {
Maybe<B> fmap<B> (Func<A,B> f);
}
public class Nothing<A> : Maybe<A> {
public Maybe<B> fmap<B>( Func<A,B> f ){
return new Nothing<B>();
}
}
Run Code Online (Sandbox Code Playgroud)
但是我得到了
`Pr.Nothing<A>' does not implement interface member `IFunctor<A>.fmap<B>
(System.Func<A,B>)' and the best implementing candidate `Pr.Nothing<A>.fmap<B>
(System.Func<A,B>)' return type `Pr.Maybe<B>' does not match interface member return
type `IFunctor<B>'
Run Code Online (Sandbox Code Playgroud)
不是Maybe<B>会员IFunctor<B>?
解
我结束了写作
public interface IFunctor<A> {
IFunctor<B> fmap<B> (Func<A,B> f);
}
public interface Maybe<A> : IFunctor<A> {
//Some stuff but not fmap
}
public class Nothing<A> : Maybe<A> {
public IFunctor<B> fmap<B>( Func<A,B> f ){
return new Nothing<B>();
}
}
Run Code Online (Sandbox Code Playgroud)
Maybe<A>.fmap()没有override IFunctor<A>.fmap().任何类型的实现Maybe<A>需要需要实现两个 Maybe<A>和IFunctor<A>.
public interface IFunctor<A>
{
IFunctor<B> fmap<B>(Func<A, B> f);
}
public interface Maybe<A> : IFunctor<A>
{
Maybe<B> fmap<B>(Func<A, B> f);
}
public class Nothing<A> : Maybe<A>
{
public Maybe<B> fmap<B>(Func<A, B> f)
{
return new Nothing<B>();
}
//This is the explicit implementation of IFunctor<A>.fmap<B>
//which in turn invokes method above.
IFunctor<B> IFunctor<A>.fmap<B>(Func<A, B> f)
{
return this.fmap(f);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
378 次 |
| 最近记录: |