开发者

overloading in different programming languages [closed]

开发者 https://www.devze.com 2023-02-03 19:33 出处:网络
开发者_运维百科As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely so
开发者_运维百科 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago.

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
0

精彩评论

暂无评论...
验证码 换一张
取 消