Multimethods are not just overloading as in C++.
To expand upon this point a little, you can use multimethods to do
pattern-matching in the style of ML and similar languages. So, to pinch
an example from the pugs tree (examples/functional/fp.p6)

multi sub length () returns Int { 0 }
multi sub length (*$x, *@xs) returns Int { 1 + length(@xs) }

Now you can call length on a list, and the call will be dispatched to
the first multi if the list is empty, the second otherwise. In both
cases you're passing a list. Really, this use is caused by having slurpy
argument lists as much as by the multis. The quicksort example in
examples/algorithms/quicksort.p6 is the clearest and most concise
description of quicksort I have yet seen. (The version I usually use
when teaching ML is not as nice as this one.)

Multis also let you define things that look like methods (in the OO
sense) outside the class. This is useful because you might want to have
common operations on certain objects be as convenient as method calls,
but without needing to give them the privileged access to the object
data that methods and submethods get, and without modifying the class

I went to the CO guess what he told me guess what he told me | apologies
He said boy u better learn to like Win no matter what u do | to Prince
But he's a fool, 'cos nothing compares, nothing compares 2 GNU
^^ http://surreal.istic.org/songs/?file=Nothing%20Compares%202%20GNU.txt

Search Discussions

Discussion Posts


Follow ups

Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 4 of 14 | next ›
Discussion Overview
groupperl6-users @
postedJun 21, '06 at 2:41p
activeJun 28, '06 at 2:20a



site design / logo © 2021 Grokbase