|
4 | 4 |
|
5 | 5 | =SUBTITLE Compile-time specification of behavior made easy.
|
6 | 6 |
|
7 |
| -In general, L<traits|https://en.wikipedia.org/wiki/Trait_(computer_programming)> |
8 |
| -are used to change the behavior of a class, by mixing in attributes and methods |
9 |
| -or changing the implementation. That kind of concept is implemented in Perl 6 |
10 |
| -via L<roles|/language/glossary#Roles>; in this context, Perl 6 calls I<traits> |
11 |
| -specifically those that change the internal representation of the class or type. |
| 7 | +In Perl 6, I<traits> are compiler hooks attached to objects and classes that |
| 8 | +modify their default behavior, functionality or representation. As such compiler |
| 9 | +hooks, they are defined in compile time, although they can be used in runtime. |
| 10 | +
|
| 11 | +=head1 The X<C<is> trait|is (trait)> |
12 | 12 |
|
13 | 13 | Traits are used extensively in L<native calls|/language/nativecall> to
|
14 |
| -L<specify the |
15 |
| -representation|/language/nativecall#Specifying_the_native_representation> of the |
| 14 | +L<specify the representation|/language/nativecall#Specifying_the_native_representation> |
| 15 | +of the |
16 | 16 | data structures that are going to be handled by the native functions via the
|
17 | 17 | C<is repr> suffix; at the same time, C<is native> is used for the routines that
|
18 | 18 | are actually implemented via native functions. These are the representations
|
@@ -67,13 +67,16 @@ methods, as above. However, trying to instantiate them this way: C<my
|
67 | 67 | $do-instantiate = don't-instantiate.new;> will yield the error C<You
|
68 | 68 | cannot create an instance of this type (don't-instantiate)>.
|
69 | 69 |
|
70 |
| -=head2 Traits and routines |
| 70 | +=head3 C<is> on routines |
71 | 71 |
|
72 |
| -Traits can be added to the definition of methods and routines to |
| 72 | +The C<is> trait can be used on the definition of methods and routines to |
73 | 73 | establish L<precedence|/functions#Precedence> and
|
74 |
| -L<associativity|/language/functions#Associativity>. They act as a L<sub |
75 |
| -defined using C<trait_mod>|/type/Sub#Traits> which take as argument the |
76 |
| -types and names of the traits that are going to be added. |
| 74 | +L<associativity|/language/functions#Associativity>. They act as a L<sub defined |
| 75 | +using C<trait_mod>|/type/Sub#Traits> which take as argument the types and names |
| 76 | +of the traits that are going to be added. In the case of subroutines, traits |
| 77 | +would be a way of adding functionality which cuts accross class and role |
| 78 | +hierarchies, or can even be used to add behaviors to independently defined |
| 79 | +routines. |
77 | 80 |
|
78 | 81 | =end pod
|
79 | 82 |
|
|
0 commit comments