Skip to content

Commit 432c409

Browse files
committed
Adds uninstantiable with examples.
This closes #1863. It advances #1957, but still some information on how to use traits in varibles and attributes is going to be needed.
1 parent c84fafe commit 432c409

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

doc/Language/traits.pod6

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,26 @@ The L<meta object protocol|/language/mop> uses it by default for every object
4444
and class unless specified otherwise; for that reason, it is in general not
4545
necessary unless you are effectively working with that interface.
4646
47+
The X<Uninstantiable representation trait> is not so much related to the
48+
representation as related to what can be done with an specific class; it
49+
effective prevents the creation of instances of the class via C<new>
50+
51+
=begin code
52+
constant @IMM = <Innie Minnie Moe>;
53+
54+
class don't-instantiate is repr('Uninstantiable') {
55+
my $.counter;
56+
57+
method imm () {
58+
return @IMM[ $.counter++ mod @IMM.elems ];
59+
}
60+
}
61+
say don't-instantiate.imm for ^10;
62+
=end code
63+
64+
Uninstantiable classes can still be used via their class variables and methods, as above. However, trying to instantiate them this way: C<my $do-instantiate = don't-instantiate.new;> will yield the error C<You cannot create an instance of this type (don't-instantiate)>.
65+
66+
4767
=end pod
4868

4969
# vim: expandtab softtabstop=4 shiftwidth=4 ft=perl6

0 commit comments

Comments
 (0)