Programmers Heaven jobs
Oracle App Openings under Train and Hire Program
Project/Program Manager
Hardware Embedded Software Engineer


More jobs                Post a job


Perl 6 FAQ - Multiple Dispatch

This FAQ is part of the Programmer's Heaven Perl 6 FAQ. It discusses multiple dispatch, including multi-subs and multi-methods.

What is multiple dispatch?

Multiple dispatch, new in Perl 6, is somewhat similar to the idea of method overloading in C# and Java. You can write multiple subroutines or methods with the same name, but with different signatures. Then, when the method is called, the number of variables being passed and their types are used to choose the correct subroutine or method to call, provided an appropriate method exists.

Note that all subroutines (or methods) with the given name must be marked with the "multi" keyword; it is not possible to have an existing subroutine or method and then add another with a different signature and just mark the one that was added as "multi". Single dispatch is the default.

Unlike static overloading in Java and C#, the decision of which method to call is made at run time. With static overloading, it is determined at compile time. Making the decision at run time introduces some overhead to the call, but clever caching techniques can help to mitigate this. It is not possible to make the decision at compile time, since new methods may be added to classes at runtime, inheritance hierarchies may change and other such evil fun.

How do I write multi-subs?

To specify that a call to a sub should do multiple dispatch, add the keyword "multi" before "sub". The following example demonstrates defining two multi-subs, both named "what_is_it" but taking parameters of different types. They are then called with parameters of different types, and the results recorded in the comment next to them.

multi sub what_is_it(Int $x) {
    say "This is the integer $x";
}
multi sub what_is_it(Str $s) {
    say "This is the string $s";
}
what_is_it(42);       # This is the integer 42
what_is_it("weasel"); # This is the string weasel


As can be seen, when an integer is passed the multi-sub taking an integer is called; when a string is passed, the multi-sub taking a string is called.

How do I write multi-methods?

Multi-methods are written by placing the "multi" keyword before "method". the main difference between multi-methods and multi-subs is that multi-methods always dispatch first like a normal method call (that is, based upon the type of the invocant). Then the possible multi-methods that could be called are searched and the most appropriate one is called.

This example is very similar to the subs one, but uses methods instead.

class Test {
    multi method what_is_it(Int $x) {
        say "This is the integer $x";
    }
    multi method what_is_it(Str $s) {
        say "This is the string $s";
    }
}
my Test $t .= new();
$t.what_is_it(42);       # This is the integer 42
$t.what_is_it("weasel"); # This is the string weasel


How do I state that only some parameters should be used for multiple dispatch?

It is possible to denote that only some of the parameters of a multi sub or multi method should be considered when doing multiple dispatch. This is indicated by placing a double semicolon after the last parameter that should be considered during dispatch.

multi sub write_to_file(str $filename ;; int mode, str $text) {
}
multi sub write_to_file(str $filename ;; str $text) {
}


Here only the first parameter will be considered when building a candidate list of subroutines that may be called. This means that the above two subroutines would always be in conflict (so in reality you could never want to write this example); without the ;; they would not be as the conflict could be resolved by considering number and types of other parameters.


Back

What's next?




Join our Perl 6 Newsletter
Email:



Visit our Perl Resources


  User Comments


riya

From India
(Report as abusive)
"Very Useful"
this faqs made to know about many things unknown and it helped me a lot
  View all   Rate and comment this article




 
Printer friendly version of the Perl6-FAQ-Multi page


Sponsored links

Build IT Knowledge with Current & Trusted Content
Helps Employees Develop & Hone New Technical Programming Skills. Sign Up & Get Full Access.
Check Out IT Certification Preparation Materials
Sign Up With SkillSoft & Get Access to Training Materials for Over 50 Professional Certifications.
SFTP components for .NET
Add complete SSH and SFTP support to your .NET framework application
Virtual File System SDK
Create your own file systems in Windows and .NET applications
PureCM Software Configuration Management
Version control and integrated issue tracking - powerful and easy to use. Get your FREE trial now!


Newsletter | Submit Content | About | Advertising | Awards | Contact Us | Link to us |
© 1996-2008 Community Networks Ltd All rights reserved. Reproduction in whole or in part, in any form or medium without express written permission is prohibited. Violators of this policy may be subject to legal action. Please read Terms Of Use and Privacy Statement for more information. Development by Synchron Data - .NET development.