Synopsis 6
by Damian Conway, Allison Randal
|
Pages: 1, 2, 3, 4
Editor's note: this document is out of date and remains here for historic interest. See Synopsis 6 for the current design information.
Attributive parameters
If a method's parameter is declared with a . after the sigil (like an
attribute):
method initialize($.name, $.age) {}
then the argument is assigned directly to the object's attribute of the same name. This avoids the frequent need to write code like:
method initialize($name, $age) {
$.name = $name;
$.age = $age;
}
Placeholder Variables
Even though every bare block is a closure, bare blocks can't have
explicit parameter lists. Instead, they use "placeholder" variables,
marked by a caret (^) after their sigils.
Using placeholders in a block defines an implicit parameter list. The signature is the list of distinct placeholder names, sorted in Unicode order. So:
{ $^y < $^z && $^x != 2 }
is a shorthand for:
sub ($x,$y,$z) { $y < $z && $x != 2 }
Types
These are the standard type names in Perl 6 (at least this week):
bit single native bit
int native integer
str native string
num native floating point
ref native pointer
bool native boolean
Bit Perl single bit (allows traits, aliasing, etc.)
Int Perl integer (allows traits, aliasing, etc.)
Str Perl string
Num Perl number
Ref Perl reference
Bool Perl boolean
Array Perl array
Hash Perl hash
IO Perl filehandle
Code Base class for all executable objects
Routine Base class for all nameable executable objects
Sub Perl subroutine
Method Perl method
Submethod Perl subroutine acting like a method
Macro Perl compile-time subroutine
Rule Perl pattern
Block Base class for all unnameable executable objects
Bare Basic Perl block
Parametric Basic Perl block with placeholder parameters
Package Perl 5 compatible namespace
Module Perl 6 standard namespace
Class Perl 6 standard class namespace
Object Perl 6 object
Grammar Perl 6 pattern matching namespace
List Perl list
Lazy Lazily evaluated Perl list
Eager Non-lazily evaluated Perl list
Value Types
Explicit types are optional. Perl variables have two associated types: their "value type" and their "variable type".
The value type specifies what kinds of values may be stored in the
variable. A value type is given as a prefix or with the returns or
of keywords:
my Dog $spot;
my $spot returns $dog;
my $spot of Dog;
our Animal sub get_pet() {...}
sub get_pet() returns Animal {...}
sub get_pet() of Animal {...}
A value type on an array or hash specifies the type stored by each element:
my Dog @pound; # each element of the array stores a Dog
my Rat %ship; # the value of each entry stores a Rat
Variable Types
The variable type specifies how the variable itself is implemented. It is given as a trait of the variable:
my $spot is Scalar; # this is the default
my $spot is PersistentScalar;
my $spot is DataBase;
Defining a variable type is the Perl 6 equivalent to tying a variable in Perl 5.
Hierarchical Types
A nonscalar type may be qualified, in order to specify what type of value each of its elements stores:
my Egg $cup; # the value is an Egg
my Egg @carton; # each elem is an Egg
my Array of Egg @box; # each elem is an array of Eggs
my Array of Array of Egg @crate; # each elem is an array of arrays of Eggs
my Hash of Array of Recipe %book; # each value is a hash of arrays of Recipes
Each successive of makes the type on its right a parameter of the
type on its left. So:
my Hash of Array of Recipe %book;
means:
my Hash(returns=>Array(returns=>Recipe)) %book;
Because the actual variable can be hard to find when complex types are specified, there is a postfix form as well:
my Hash of Array of Recipe %book; # HoHoAoRecipe
my %book of Hash of Array of Recipe; # same thing
my %book returns Hash of Array of Recipe; # same thing
The returns form is more commonly seen in subroutines:
my Hash of Array of Recipe sub get_book () {...}
my sub get_book () of Hash of Array of Recipe {...}
my sub get_book returns Hash of Array of Recipe {...}
Junctive Types
Anywhere you can use a single type you can use a junction of types:
my Int|Str $error = $val; # can assign if $val~~Int or $val~~Str
if $shimmer.isa(Wax & Topping) {...} # $shimmer must inherit from both
Parameter Types
Parameters may be given types, just like any other variable:
sub max (int @array is rw) {...}
sub max (@array of int is rw) {...}
Return Types
On a scoped subroutine, a return type can be specified before or after the name:
our Egg sub lay {...}
our sub lay returns Egg {...}
my Rabbit sub hat {...}
my sub hat returns Rabbit {...}
If a subroutine is not explicitly scoped, then it belongs to the current namespace (module, class, grammar, or package). Any return type must go after the name:
sub lay returns Egg {...}
On an anonymous subroutine, any return type can only go after the name:
$lay = sub returns Egg {...};
unless you use the "anonymous declarator" (a/an):
$lay = an Egg sub {...};
$hat = a Rabbit sub {...};
Properties and Traits
Compile-time properties are now called "traits." The
is NAME (DATA) syntax defines traits on containers and
subroutines, as part of their declaration:
my $pi is constant = 3;
my $key is Persistent(file=>".key");
sub fib is cached {...}
The will NAME BLOCK syntax is a synonym for is NAME (BLOCK):
my $fh will undo { close $fh }; # Same as: my $fh is undo({ close $fh });
The but NAME (DATA) syntax specifies run-time properties on values:
my $pi = 3 but Approximate("legislated");
sub system {
...
return $error but false if $error;
return 0 but true;
}
Subroutine Traits
These traits may be declared on the subroutine as a whole (not on individual parameters).
is signature
- The signature of a subroutine -- normally declared implicitly, by providing a parameter list and/or return type.
returns/is returns
- The type returned by a subroutine.
will do
- The block of code executed when the subroutine is called -- normally declared implicitly, by providing a block after the subroutine's signature definition.
is rw
- Marks a subroutine as returning an lvalue.
is parsed
- Specifies the rule by which a macro call is parsed.
is cached
- Marks a subroutine as being memoized
is inline
- Suggests to the compiler that the subroutine is a candidate for optimization via inlining.
is tighter/is looser/is equiv
- Specifies the precedence of an operator relative to an existing operator.
is assoc
- Specifies the associativity of an operator.
PRE/POST
-
Mark blocks that are to be unconditionally executed before/after
the subroutine's
doblock. These blocks must return a true value, otherwise an exception is thrown. FIRST/LAST/NEXT/KEEP/UNDO/etc.
-
Mark blocks that are to be conditionally executed before or after
the subroutine's
doblock. The return values of these blocks are ignored.





