Skip to content

Commit 2cd9fd0

Browse files
committed
Reorganizes and adds info on «is» for classes. Refs #1957
1 parent 02669a0 commit 2cd9fd0

File tree

1 file changed

+57
-28
lines changed

1 file changed

+57
-28
lines changed

doc/Language/traits.pod6

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,41 +8,33 @@ In Perl 6, I<traits> are compiler hooks attached to objects and classes that
88
modify their default behavior, functionality or representation. As such compiler
99
hooks, they are defined in compile time, although they can be used in runtime.
1010
11+
Several traits are already defined as part of the language or the Rakudo
12+
compiler by using the C<trait_mod> keyword. They are listed, and explained,
13+
next.
14+
1115
=head1 The X<C<is> trait|is (trait)>
1216
13-
Traits are used extensively in L<native calls|/language/nativecall> to
14-
L<specify the representation|/language/nativecall#Specifying_the_native_representation>
15-
of the
16-
data structures that are going to be handled by the native functions via the
17-
C<is repr> suffix; at the same time, C<is native> is used for the routines that
18-
are actually implemented via native functions. These are the representations
19-
that can be used:
17+
Defined as
2018
21-
=item X<CStruct> corresponds to a C<struct> in the C language. It is a composite
22-
data structure which includes different and heterogeneous lower-level data
23-
structures; see L<this|/language/nativecall#Structs> for examples and further
24-
explanations.
19+
proto sub trait_mod:<is>(Mu $, |) {*}
2520
26-
=item X<CPPStruct>, similarly, correspond to a C<struct> in C++. However, this
27-
is Rakudo specific for the time being.
21+
C<is> applies to any kind of scalar object, and can take any number of named or
22+
positional arguments. It is the most commonly used trait, and takes the
23+
following forms, depending on the type of the first argument.
2824
29-
=item X<CPointer> is a pointer in any of these languages. It is a dynamic data
30-
structure that must be instantiated before being used, can be
31-
L<used|/language/nativecall#Basic_use_of_Pointers> for classes whose methods are
32-
also native.
25+
=head2 C<is> applied to classes.
3326
34-
=item X<CUnion> is going to use the same representation as an C<union> in C; see
35-
L<this|/language/nativecall#CUnions> for an example.
27+
The most common form, involving two classes, one that is being defined and the
28+
other existing, L<defines parenthood|/syntax/is>. C<A is B>, if both are
29+
classes, defines A as a subclass of B.
3630
37-
On the other hand, X<P6opaque> is the default representation used for all
38-
objects in Perl 6.
39-
40-
m: class Thar {};
41-
say Thar.REPR; #OUTPUT: «P6opaque␤»
31+
L<C<is DEPRECATED>|/Attribute#trait_is_DEPRECATED> can be applied to classes,
32+
Attributes or Routines, marks them as deprecated and issues a message, if
33+
provided.
4234
43-
The L<meta object protocol|/language/mop> uses it by default for every object
44-
and class unless specified otherwise; for that reason, it is in general not
45-
necessary unless you are effectively working with that interface.
35+
Several instances of C<is> are translated directly into attributes for the class
36+
they refer to: C<rw>, C<nativesize>, C<ctype>, C<unsigned>, C<hidden>,
37+
C<array_type>.
4638
4739
The X<Uninstantiable representation trait> is not so much related to the
4840
representation as related to what can be done with an specific class; it
@@ -67,7 +59,44 @@ methods, as above. However, trying to instantiate them this way: C<my
6759
$do-instantiate = don't-instantiate.new;> will yield the error C<You
6860
cannot create an instance of this type (don't-instantiate)>.
6961
70-
=head3 C<is> on routines
62+
=head2 C<is repr> and native representations.
63+
64+
Since the C<is> trait refers, in general, to the nature of the class or object
65+
they are applied to, they are used extensively in
66+
L<native calls|/language/nativecall> to L<specify the representation|/language/nativecall#Specifying_the_native_representation>
67+
of the data structures that are going to be handled by the native functions via
68+
the C<is repr> suffix; at the same time, C<is native> is used for the routines
69+
that are actually implemented via native functions. These are the
70+
representations that can be used:
71+
72+
=item X<CStruct> corresponds to a C<struct> in the C language. It is a composite
73+
data structure which includes different and heterogeneous lower-level data
74+
structures; see L<this|/language/nativecall#Structs> for examples and further
75+
explanations.
76+
77+
=item X<CPPStruct>, similarly, correspond to a C<struct> in C++. However, this
78+
is Rakudo specific for the time being.
79+
80+
=item X<CPointer> is a pointer in any of these languages. It is a dynamic data
81+
structure that must be instantiated before being used, can be
82+
L<used|/language/nativecall#Basic_use_of_Pointers> for classes whose methods are
83+
also native.
84+
85+
=item X<CUnion> is going to use the same representation as an C<union> in C; see
86+
L<this|/language/nativecall#CUnions> for an example.
87+
88+
On the other hand, X<P6opaque> is the default representation used for all
89+
objects in Perl 6.
90+
91+
m: class Thar {};
92+
say Thar.REPR; #OUTPUT: «P6opaque␤»
93+
94+
The L<meta object protocol|/language/mop> uses it by default for every object
95+
and class unless specified otherwise; for that reason, it is in general not
96+
necessary unless you are effectively working with that interface.
97+
98+
99+
=head2 C<is> on routines
71100
72101
The C<is> trait can be used on the definition of methods and routines to
73102
establish L<precedence|/functions#Precedence> and

0 commit comments

Comments
 (0)