instance declarations

Ketil Z Malde
10 Dec 2001 10:32:48 +0100

David Feuer <> writes:

> 1.  Why can't [instances] be hidden in module imports/exports?

The way I see it, an instance declaration is an assertion that a
certain data type supports a certain set of operations.  Thus, if the
data type and the operations on it are in scope, it makes sense for
the class instance to be, too.

(This leads to the question of why we need to have instance
declarations at all :-)  (My guesses would be: compiler
implementation issues, code clarity, error detection, partially
implemented classes))

Problems arise when a data type needs to be instantiated twice in the
same class, but with different operator implementations.  (I.e. you
have a data type which prints differently according to which module
you're in)

This would, I think, be a problem in most languages, think of deriving
a C++ class twice from the same base class while providing different
overrides for the functions.

I'm not entirely convinced it's an issue that needs a better
resolution than the language provides today.  (There's a diminishing
returns effect when adding language features, and at some point, the
increased complexity of the language doesn't make it worth it, IMHO.)

> 2.  Why can't you simultaneously declare a type to be an instance of
> multiple classes?

Why does it matter?

> class C1 t where a::t->t
> class C1 t => C2 t where b::t->t
> instance C1 T, C2 T where
> 	a=...
> 	b=...

To me, it'd make more sense to provide

class C1 t where a :: t -> t
class (C1 t) => C2 t where b :: t -> t

instance C2 T where
	a = ...  -- implicitly instantiating C1
	b = ...

and avoid long instantiation chains.  But that too is IMHO a minor issue.

If I haven't seen further, it is by standing in the footprints of giants