C#接口和Haskell类型类之间的区别 [英] Difference between C# interface and Haskell Type Class

查看:63
本文介绍了C#接口和Haskell类型类之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这里有一个类似的问题,但是我想看一个示例,该示例清楚地显示了interface不能使用类型类实现的功能

I know that there is a similar question here, but I would like to see an example, which clearly shows, what you can not do with interface and can with Type Class

为进行比较,我将为您提供示例代码:

For comparison I'll give you an example code:

class Eq a where 
    (==) :: a -> a -> Bool
instance Eq Integer where 
    x == y  =  x `integerEq` y

C#代码:

interface Eq<T> { bool Equal(T elem); }
public class Integer : Eq<int> 
{
     public bool Equal(int elem) 
     {
         return _elem == elem;
     }
}

如果无法正确理解,请更正我的示例

Correct my example, if not correctly understood

推荐答案

在对这个问题进行了长期研究之后,我得出了一种简单的解释方法.至少对我来说很清楚.

After a long study of this issue, I came to an easy method of explaining. At least for me it's clear.

想象一下我们有这样的签名方法

Imagine we have method with signature like this

public static T[] Sort(T[] array, IComparator<T> comparator) 
{
    ...
}

IComparator的实现:

public class IntegerComparator : IComparator<int> { }

然后我们可以编写如下代码:

Then we can write code like this:

var sortedIntegers = Sort(integers, new IntegerComparator());

我们可以改进此代码,首先创建Dictionary<Type, IComparator>并填充它:

We can improve this code, first we create Dictionary<Type, IComparator> and fill it:

var comparators = new Dictionary<Type, IComparator>() 
{
    [typeof(int)]    = new IntegerComparator(),
    [typeof(string)] = new StringComparator() 
}

重新设计了IComparator接口,以便我们可以像上面那样编写

Redesigned IComparator interface so that we could write like above

public interface IComparator {}
public interface IComparator<T> : IComparator {}

然后,我们重新设计Sort方法签名

And after this let's redesign Sort method signature

public class SortController
{
    public T[] Sort(T[] array, [Injectable]IComparator<T> comparator = null) 
    {
        ...
    }
}

如您所知,我们将注入IComparator<T>,并编写如下代码:

As you understand we are going to inject IComparator<T>, and write code like this:

new SortController().Sort<int>(integers, (IComparator<int>)_somparators[typeof(int)])

正如您已经猜到的那样,在我们概述实现并添加Dictionary<Type, IComparator>

As you already guessed this code will not work for other types until we outline the implementation and add in Dictionary<Type, IComparator>

注意,只有在运行时才能看到的异常

Notice, the exception we will see only on runtime

现在想象一下,如果编译器在构建期间为我们完成了这项工作,并且如果找不到具有相应类型的比较器,则会抛出异常.

And now imagine if this work was done for us by the compiler during build and it threw exception if it could not find the comparator with corresponding types.

为此,我们可以帮助编译器并添加一个新的关键字而不是用法属性. Sort方法将如下所示:

For this, we could help the compiler and add a new keyword instead of usage attribute. Out Sort method will be look like this:

public static T[] Sort(T[] array, implicit IComparator<T> comparator) 
{
    ...
}

与实现代码的具体比较器:

And code of realization concrete Comparator:

public class IntegerComparator : IComparator<int> implicit { }

注意,我们使用关键字'implicit',此编译器之后将可以执行 我们在上面写过的常规工作,并且在执行过程中会抛出异常 编译时

Note, we use the keyword 'implicit', after this compiler will be able to do routine work, which we wrote above, and the exception will be thrown during compile-time

var sortedIntegers = Sort(integers);

// this gives us compile-time error
// because we don't have implementation of IComparator<string> 
var sortedStrings = Sort(strings); 

并为这种实现方式命名 Type Class

And give the name to this style of implementation Type Class

public class IntegerComparator : IComparator<int> implicit { }

我希望我理解正确并且可以理解.

I hope that I understood correctly and understandably explained.

PS:该代码无法伪装.

PS: The code does not pretend to work.

这篇关于C#接口和Haskell类型类之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆