Why does perl6 multi default to sub?

  • A+
Category:Languages

In reference to this question / answer, perl6 multi defaults to sub.

No such method <name> for invocant of type <class>

I would have expected it to default to method. Please could someone explain the rationale for this?

 


A sub can occur anywhere and defaults to lexical scoping rules (my).
A method usually occurs only in a class definition and it defaults to being scoped to a class (has).

my multi sub foo (){…} multi foo (){…} # identical  my multi method foo (){…} # must be forced to my declaration   class :: {   my multi sub foo (){…}   multi foo (){…} # identical    has multi method bar (){…}   multi method bar (){…} # identical }   sub :: () {   my multi sub foo (){…}   multi foo (){…} # identical    my multi method foo (){…} # must be forced to my declaration }  method :: () {   my multi sub foo (){…}   multi foo (){…} # identical    my multi method foo (){…} # must be forced to my declaration } 

Since a sub can, and does occur everywhere, it makes more sense for multi to imply a sub rather than method.

While you may think that it would make sense for multi to imply a method in a class definition; that would make it so that multi implies two different things (times two) depending on context. It would imply my sub outside of a class, and has method inside of a class. (It would also go back to my sub inside of a method inside of a class.)

That is it would be a special case. One of the design goals of Perl 6 is to reduce the number of special cases.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: