Can somebody please explain (with example) the difference between context-independent and context-dependent overloading?
I have never heard about those. And there's only about five hits on Google, one of which is this very question, which seems to suggest to me that these are made-up terms. And as with any made-up term, if you want to know what it means, you have to ask the person who made it up.
From what little I could gather, it seems to be related to return-type based overloading.
Basically, if you have four overloaded functions like these:
foo :: string → int
foo :: string → string
foo :: string → ()
foo :: int → int
And you call them like this:
1 + foo 1
1 + foo "one"
foo "one"
Then, with context-dependent overloading (i.e. overloading based on the return type as well as the parameter types), the following implementations will be selected:
1 + foo 1 # foo :: int → int
1 + foo "one" # foo :: string → int (because `+` takes an `int`)
foo "one" # foo :: string → () (because there is no return value)
Whereas with context-independent overloading (i.e. ignoring the return type), the following implementations will be selected:
1 + foo 1 # foo :: int → int
1 + foo "one" # ERROR
foo "one" # ERROR
In both the ERROR
cases, there is an ambiguity between foo :: string → int
, foo :: string → string
and foo :: string → ()
, since they only differ in their return type but have the same paremeter type.
Quoting from here:
There are two kinds of overloading of functions/operators.
- context-independent - overloading only done on parameters to function or type of operands for an operator
- context-dependent - which abstraction to call also depends upon the type of the result
精彩评论