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
,它可以像普通的用户定义类型一样使用