@@ -22,29 +22,33 @@ object. That includes literals, types (type objects), code and containers.
22
22
23
23
To call a method on an object, add a dot, followed by the method name:
24
24
25
- = for code :allow<B L>
26
- L < say > "abc"B < . L < uc > > ;
25
+ = for code
26
+ say "abc".uc ;
27
27
# OUTPUT: «ABC»
28
28
29
- This calls the L < C < uc > | uc> method on C < "abc" > , which is an object of type
30
- L < C < Str > | Str> . To supply arguments to the method, add arguments inside parentheses
29
+ This calls the C < uc > method on C < "abc" > , which is an object of type
30
+ C < Str > . To supply arguments to the method, add arguments inside parentheses
31
31
after the method.
32
32
33
- = for code :allow<B L>
34
- my $formatted-text = "Fourscore and seven years ago...".L < indent > B < (8) > ;
33
+ = for code
34
+ my $formatted-text = "Fourscore and seven years ago...".indent(8);
35
+ say $formatted-text;
36
+ # OUTPUT: « Fourscore and seven years ago...»
35
37
36
38
C < $formatted-text > now contains the above text, but indented 8 spaces.
37
39
38
40
Multiple arguments are separated by commas:
39
41
40
- = for code :allow<B L> : preamble<my $formatted-text;>
42
+ = for code :preamble<my $formatted-text;>
41
43
my @words = "Abe", "Lincoln";
42
- @words.L < push > ("said"B < , > $formatted-text.L < comb > (L < /\w+/|/language/regexes > ));
44
+ @words.push("said", $formatted-text.comb(/\w+/));
45
+ say @words;
46
+ # OUTPUT: «[Abe Lincoln said (Fourscore and seven years ago)]»
43
47
44
48
Multiple arguments can be specified by separating the argument list with a colon:
45
49
46
- = for code :allow<B L> : preamble<my @words;>
47
- say @words.L < join > : '--';
50
+ = for code :preamble<my @words;>
51
+ say @words.join: '--';
48
52
# OUTPUT: «Abe--Lincoln--said--Fourscore--and--seven--years--ago»
49
53
50
54
Since you have to put a C < : > after the method if you want to pass arguments
@@ -63,12 +67,11 @@ Methods can return mutable containers, in which case you can assign to the
63
67
return value of a method call. This is how read-writable attributes to
64
68
objects are used:
65
69
66
- = for code :allow<L>
67
- $*IN.L < nl-in > = "\r\n";
70
+ = for code
71
+ $*IN.nl-in = "\r\n";
68
72
69
73
Here, we call method C < nl-in > on the C < $*IN > object, without arguments,
70
- and assign to the container it returned with the
71
- L < C < = > |=> operator.
74
+ and assign to the container it returned with the L < C < = > |=> operator.
72
75
73
76
All objects support methods from class L < Mu > , which is the type hierarchy root.
74
77
All objects derive from C < Mu > .
@@ -77,46 +80,42 @@ All objects derive from C<Mu>.
77
80
78
81
Types themselves are objects and you can get the I < type object > by writing its name:
79
82
80
- my $int-type-obj = Int;
83
+ = for code
84
+ my $int-type-obj = Int;
81
85
82
86
You can ask any object for its type object by calling the C < WHAT > method
83
87
(which is actually a macro in method form):
84
88
85
89
= for code :ok-test<WHAT>
86
- my $int-type-obj = 1.WHAT;
90
+ my $int-type-obj = 1.WHAT;
87
91
88
92
Type objects (other than L < Mu > ) can be compared for equality with the
89
93
L < C < === > |===> identity operator:
90
94
91
- = begin code :allow<B L> :ok-test<WHAT>
92
-
95
+ = for code :ok-test<WHAT>
93
96
sub f(Int $x) {
94
- if $x.WHAT B < L < === > > Int {
97
+ if $x.WHAT === Int {
95
98
say 'you passed an Int';
96
99
}
97
100
else {
98
101
say 'you passed a subtype of Int';
99
102
}
100
103
}
101
- = end code
102
104
103
105
Although, in most cases, the L < C < .isa > |isa> method will suffice:
104
106
105
- = begin code :allow<B L>
106
-
107
+ = for code
107
108
sub f($x) {
108
- if $x B < . L < isa > > (Int) {
109
+ if $x. isa(Int) {
109
110
...
110
111
}
111
112
...
112
113
}
113
114
114
- = end code
115
-
116
- Subtype checking is done by smart-matching:
115
+ Subtype checking is done by X < smart-matching|smartmatch operator > :
117
116
118
- = for code :allow<B L> : preamble<my $type;>
119
- if $type B < L < ~~ > > L < Real > {
117
+ = for code :preamble<my $type;>
118
+ if $type ~~ Real {
120
119
say '$type contains Real or a subtype thereof';
121
120
}
122
121
0 commit comments