
On May 28, 2013, at 12:36 AM, harry
Every OO language which supports generics allows a declaration such as List<Show> alist, where Show is an interface. Any type implementing Show can be put in alist, and any Show operation can be performed on the alist's members. No casts, wrappers, or other special types and plumbing are needed.
Why isn't it possible to do this directly in Haskell?
My impression is that you often don't have this requirement, since functions are first-class values and you can treat them as closures. Consider this (silly) C++:
struct Iface {
// Yields the length of the printable representation of some object, plus some custom value.
virtual int lengthPlus( int i ) = 0;
};
struct MyString : Iface {
MyString( const std::string &s ) : m_s( s ) { }
virtual int lengthPlus( int i ) {
return m_s.size() + i;
}
std::string m_s;
};
struct MyBoolean : Iface {
MyBoolean( bool b ) : m_b( b ) { }
virtual int lengthPlus( int i ) {
return m_b ? strlen( "True" ) + i
: strlen( "False" ) + i;
}
bool m_b;
};
You could now have code like:
std::list