Is it possible to write a functor Interface for .NET?

后端 未结 2 797
故里飘歌
故里飘歌 2021-01-15 13:41

Functional languages often have Functor types/interfaces.

In .NET a Functor interface would be an Interface implementable by generic types (T)

2条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-15 13:58

    The difficulty of giving a type to fmap is that it is parameterized by a higher-kinded type variable, the container type. So beyond being a simple polymorphic function, like say, map on lists:

    map :: (a -> b) -> List a -> List b
    

    fmap generalizes this to work on any container, as well as any element type:

    fmap :: Functor (f :: * -> *) => (a -> b) -> f a -> f b
    

    The problem in C# is that f is a higher-kinded type variable (it is a type function). Since C# doesn't support higher kinds, you'll be out of luck writing this directly, think.

提交回复
热议问题