I am trying to implement an IFunctor interface [Haskell's Fuctor class] and a Maybe interface to create two classes: Just and Nothing.
So far I have:
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>();
}
}
However I get
`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>'
Isn't Maybe<B> a member of IFunctor<B>?
Solution
I ended writing
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>();
}
}
fmap<B> (Func<A,B> f)so you need implement this function in derived class for each interface, otherwise you can get error when try(IFunctor<A>)Nothing<A>