Skip to content

Commit 9231982

Browse files
committed
start "objects" language documentation
1 parent 6fe21c0 commit 9231982

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

lib/objects.pod

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
=begin pod
2+
3+
=TITLE Object Orientation in Perl 6
4+
5+
Perl 6 is an object oriented language at its core, even though it
6+
allows you to write programs in other programming styles.
7+
8+
Perl 6 comes with a wealth of predefined types, which can be classified
9+
in two categories: normal and I<native> types.
10+
11+
Native types are used for low-level types (like C<uint64>). They do not
12+
have the same capabilities as objects, though if you call methods on them,
13+
they are I<boxed> into normal objects.
14+
15+
Everything that you can store in a variable is either a native value, or an
16+
object. That includes literals, types (type objects), code and containers.
17+
18+
=head1 Using Objects
19+
20+
You can use objects by calling methods on them. To call a method on an
21+
expression, add a dot, followed by the method name, optionally followed by
22+
its argument list in round parenthesis (note that no whitespace is allowed
23+
between the method name and the argument list):
24+
25+
say "abc".uc;
26+
# ^^^ method call without arguments
27+
my @words = $string.comb(/\w+/);
28+
# ^^^^^^^^^^^^ method call with one argument
29+
30+
Another method call syntax separates the method name and the argument list
31+
with a colon:
32+
33+
say @*INC.join: ':';
34+
35+
Many operations that don't look like method calls (for example smart
36+
matching, interpolating an object into a string) results in method
37+
calls under the hood.
38+
39+
Methods can return mutable containers, in which case you can assign
40+
to the return value of a method call.
41+
42+
$*IN.input-line-separator = "\r\n";
43+
44+
All objects support the methods from class L<Mu>, which is the root
45+
of the type hierarchy.
46+
47+
=head2 Type Objects
48+
49+
Types themselves are objects, and you can get the I<type object> simply
50+
by writing its name:
51+
52+
my $int-type-obj = Int;
53+
54+
You can ask any object for its type object by calling the C<WHAT> method
55+
(which is actually a macro in method form):
56+
57+
my $int-type-obj = 1.WHAT;
58+
59+
Type objects (other than C<Mu>) can be compared for equality with the
60+
C<===> identity operator:
61+
62+
sub f(Int $x) {
63+
if $x.WHAT == Int {
64+
say 'you passed an Int';
65+
}
66+
else {
67+
say 'you passed a subtype of Int';
68+
}
69+
}
70+
71+
Subtype checking is done by smart-matching:
72+
73+
if $type ~~ Real {
74+
say '$type contains Real or a subtype thereof';
75+
}
76+
77+
=head1 Classes
78+
79+
TODO: attributes, methods, object construction, inheritance
80+
81+
=head1 Roles
82+
83+
TODO: basics, conflicts, parameterized, difference between classes and roles
84+
85+
=head1 Meta-Object Programming and Introspection
86+
87+
TODO: everything :-)
88+
89+
=end pod

0 commit comments

Comments
 (0)