Christian Schneider wrote:
Michael Walter wrote:
How exactly do you think are default parameters related to the issue,
anyway?
Java uses multi-method dispatch to work around missing default values:
function foo($a, $some_flag) { ... }
function foo($a) { foo($a, false); }
instead of
function foo($a, $some_flag = false) { ... }
Well yeah, we were presumably talking about different things, hence the
question :) What you are talking about is a certain kind of "compile
time" polymorphism, where the appropriate method is chosen at compile time.
I was referring to "runtime" polymorphism, i.e. the _dispatch_ is done
on not only on one type (as with the type of the $object in an
$object->method() call), but on 2 types (as in multimethod($rect,$circle)).
I'm pretty sure that Sebastian was referring to the same mechanism, as
his example would need dispatch on 2 types, too, in order to work.
Also, I think functions which are dispatched using multiple types might
as well be global functions, because usually (?) there is no such thing
as a "first class" and "second class" dispatchee (is that a word?). For
instance, consider:
function collides(Rect $rect, Rect $circle) { ... }
function collides(Rect $rect, Circle $circle) { ... }
I assume you are you aware of what dynamic dispatch is trying to solve
Whatever example I'd give you'd probably say it's not the relevant one.
Naw, I didn't want to piss you of, just check whether we were dealing
with the same thing. :)
But if you give me a use case for dynamic dispatch I show you how I'd do
it in PHP. Maybe we should take that off list though and present just
the result of our research :-)
Yeah, well, you will show me the visitor pattern, which is an inferior
work-around for the lack of multiple dispatch (because it only works
well for closed sets of classes, although having it work for an open set
of classes like with dynamic dispatch on the $this 'pointer' is desirable).
For a typical example, you might want to check out the Gang of Four book
(Visitor pattern, which simulates multiple dispatch for multiple = 2) or
consider above's collides() example.
The main difference between the 'isinstance' solution someone on this
list posted is that you do not have to _modify_ a big large collides()
function, but you just keep adding the new cases.
This is like with inheritance -- you use inheritance and dynamic
dispatch on the $this 'pointer' in order to avoid series of 'isinstance'
checks.
Anyway, I'm not really sure whether dynamic dispatch is appropriate in
PHP myself, but I felt like pointing out the differences between static
and dynamic dispatch might be important. ;)
Cheers,
Michael