Types 有界参数多态性与自组织多态性

Types 有界参数多态性与自组织多态性,types,polymorphism,Types,Polymorphism,这里已经回答了一个类似的问题-。这个问题的区别在于有界这个术语。似乎有两种不同的特殊多态性- 例如,在C++中,你可以写 int foo(int x) { return x; } int foo(string x) { return x.size(); } class Foo a where foo :: a -> Int instance Foo Int where foo x = x instance Foo String where foo xs = length x

这里已经回答了一个类似的问题-。这个问题的区别在于有界这个术语。似乎有两种不同的特殊多态性-

例如,在C++中,你可以写

int foo(int x) { return x; }
int foo(string x) { return x.size(); }
class Foo a where
  foo :: a -> Int
instance Foo Int where
  foo x = x
instance Foo String where
  foo xs = length xs
在这种情况下,将
foo
的类型称为重载集是没有意义的(从某种意义上说,它不是用户可定义的类型,或者您可以为其创建别名的类型),但将单个重载的类型本身说成是有意义的

  • 在哈斯克尔,你可以写作

    int foo(int x) { return x; }
    int foo(string x) { return x.size(); }
    
    class Foo a where
      foo :: a -> Int
    instance Foo Int where
      foo x = x
    instance Foo String where
      foo xs = length xs
    
    在这里,将
    foo
    的类型本身说成是一个合适的类型
    fooa=>a->Int
    ,它可以像普通的用户定义类型一样使用

  • 这样说是否恰当:

  • C++通过函数重载实现的即席多态性不能归类为有界参数多态性

  • Haskell通过类型类实现的即席多态性可以归类为有界参数多态性(另一个例子是,Pierce的类型和编程语言谈到System-F)