Skip to content

Commit 470b94a

Browse files
committed
Clean up / unify meta model docs
* move several methods in the appropriate classes * avoid duplicate descriptions between Language/objects and type docs * document several more methods
1 parent cd28fdf commit 470b94a

File tree

5 files changed

+182
-81
lines changed

5 files changed

+182
-81
lines changed

lib/Language/objects.pod

Lines changed: 2 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -620,86 +620,7 @@ To get rid of using the same object twice, there is a shortcut:
620620
# same as
621621
say 1.HOW.name(1); # Int
622622
623-
=head2 class Perl6::Metamodel::ClassHOW
624-
625-
=SUBTITLE Meta object for a class
626-
627-
Introspection is the process of getting information about an object or class
628-
at runtime. In Perl 6, all introspection goes through the meta object. The
629-
standard C<ClassHOW> for class-based objects offers these facilities:
630-
631-
=head3 method can
632-
633-
Given a method names, it returns a L<Parcel> of methods that are available
634-
with this name.
635-
636-
class A { method x($a) {} };
637-
class B is A { method x() {} };
638-
say B.^can('x').elems; # 2
639-
for B.^can('x') {
640-
say .arity; # 1, 2
641-
}
642-
643-
In this example, class C<B> has two possible methods available with name C<x>
644-
(though a normal method call would only invoke the one installed in C<B>
645-
directly). The one in C<B> has arity 1 (i.e. it expects one argument, the
646-
invocant (C<self>)), and the one in C<A> expects 2 arguments (C<self> and
647-
C<$a>).
648-
649-
=head3 method methods
650-
651-
Returns a list of public methods available on the class (which includes
652-
methods from superclasses and roles). By default this stops at the classes
653-
L<Cool>, L<Any> or L<Mu>; to really get all methods, use the C<:all> adverb.
654-
655-
class A {
656-
method x() { };
657-
}
658-
659-
say A.^methods(); # x
660-
say A.^methods(:all); # x infinite defined ...
661-
662-
The returned list actually returns objects of type L<Method>, which you can
663-
use to introspect their signatures, and even to call them.
664-
665-
=head3 method mro
666-
667-
Returns the list of the class itself and its superclasses in method resolution
668-
order. Perl 6 linearizes the graph of superclasses into a single list that
669-
C<$object.^mro> returns; when a method is called, the class and its parent
670-
classes are visited in that order. (Only conceptually; actually the list of
671-
methods is built at class composition time).
672-
673-
say 1.^mro; # (Int) (Cool) (Any) (Mu)
674-
675-
=head3 method name
676-
677-
Returns the name of the class:
678-
679-
say 'a string'.^name; # Str
680-
681-
=head3 method parents
682-
683-
Returns the list of parent classes. By default it stops at L<Cool>, L<Any> or
684-
L<Mu>, which you can suppress by supplying the C<:all> adverb. With C<:tree>,
685-
a nested list is returned.
686-
687-
class D { };
688-
class C1 is D { };
689-
class C2 is D { };
690-
class B is C1 is C2 { };
691-
class A is B { };
692-
693-
say A.^parents(:all).perl; # (B, C1, C2, D, Any, Mu)
694-
say A.^parents(:all, :tree).perl;
695-
# ([B, [C1, [D, [Any, [Mu]]]], [C2, [D, [Any, [Mu]]]]],)
696-
697-
=head3 method attributes
698-
699-
Even though attributes are private to a class, you can ask a class for its
700-
attributes with C<$object.^attributes>. Each attribute is represented by an
701-
instance of the Attribute class and has attributes of its own. Of particular
702-
interest are C<name>, C<rw> and C<ro>, C<has_accessor>, C<type>,
703-
and C<package>.
623+
See L<Metamodel::ClassHOW|/type/Metamodel::ClassHOW> for documentation of
624+
the meta class of C<class>.
704625
705626
=end pod
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
=begin pod
2+
3+
=TITLE role Metamodel::AttributeContainer
4+
5+
=SUBTITLE Meta role for holding attributes
6+
7+
role Metamodel::AttributeContainer { ... }
8+
9+
Classes, roles and grammars can have attributes. Storage and introspection of
10+
attributes is implemented by this role.
11+
12+
=head1 Methods
13+
14+
=head2 method add_attribute
15+
16+
method add_attribute(Metamodel::AttributeContainer: $obj, $name, $attribute)
17+
18+
Adds an attribute. C<$attribute> must be an object that supports the
19+
methods C<name>, C<type> and C<package>, which are called without arguments.
20+
It can for example be of L<type Attribute|/type/Attribute>.
21+
22+
=head2 method attributes
23+
24+
method attributes(Metamodel::AttributeContainer: $obj)
25+
26+
Returns a list of attributes. For most Perl 6 types, these will be objects of
27+
L<type Attribute|/type/Attribute>.
28+
29+
=begin comment
30+
31+
TODO: compose_attributes, set_rw, rw, get_attribute_for_usage
32+
33+
Also TODO: desribe :local, :excl, :all options of method attributes
34+
35+
=end comment
36+
37+
=end pod

lib/Type/Metamodel/ClassHOW.pod

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,24 @@ L<of type X::Method::NotFound|/type/X::Method::NotFound> is thrown.
5858
User-facing code (that is, ,code not dabbling with meta classes) should use
5959
method C<FALLBACK> instead.
6060
61+
=head2 method can
62+
63+
method can(Metamodel::ClassHOW:D: $obj, $method-name)
64+
65+
Given a method names, it returns a L<Parcel> of methods that are available
66+
with this name.
67+
68+
class A { method x($a) {} };
69+
class B is A { method x() {} };
70+
say B.^can('x').elems; # 2
71+
for B.^can('x') {
72+
say .arity; # 1, 2
73+
}
74+
75+
In this example, class C<B> has two possible methods available with name C<x>
76+
(though a normal method call would only invoke the one installed in C<B>
77+
directly). The one in C<B> has arity 1 (i.e. it expects one argument, the
78+
invocant (C<self>)), and the one in C<A> expects 2 arguments (C<self> and
79+
C<$a>).
80+
6181
=end pod
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
=begin pod
2+
3+
=TITLE role Metamodel::MethodContainer
4+
5+
=SUBTITLE Meta role that handles storing and introspecting methods
6+
7+
class Metamodel::MethodContainer { ... }
8+
9+
roles, classes, grammars ane enums can contain methods. This role implements
10+
the API around storing and introspecting them.
11+
12+
say .name for Int.^methods(:all);
13+
14+
# don't do that, because it changes type Int globally.
15+
# just for demonstration purposes.
16+
Int.^add_method('double', method ($x:) { 2 * $x });
17+
say 21.double;
18+
19+
=head1 Methods
20+
21+
=head2 method add_method
22+
23+
method add_method(Metamodel::MethodContainer: $obj, $name, $code)
24+
25+
Adds a method to the meta class, to be called with name C<$name>.
26+
This should only be done before a type is composed.
27+
28+
=head2 method methods
29+
30+
method methods(Metamodel::MethodContainer: $obj, :$all, :$local)
31+
32+
Returns a list of public methods available on the class (which includes
33+
methods from superclasses and roles). By default this stops at the classes
34+
L<Cool>, L<Any> or L<Mu>; to really get all methods, use the C<:all> adverb.
35+
If C<:local> is set, only methods declared directly in the class are returned.
36+
37+
class A {
38+
method x() { };
39+
}
40+
41+
say A.^methods(); # x
42+
say A.^methods(:all); # x infinite defined ...
43+
44+
The returned list contains objects of type L<Method>, which you can
45+
use to introspect their signatures and call them.
46+
47+
=begin comment
48+
49+
TODO: method_table, submethod_table, declares_method, lookup, cache,
50+
cache_get, cache_add
51+
52+
=end comment
53+
54+
=end pod
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
=begin pod
2+
3+
=TITLE role Metamodel::MultipleInheritance
4+
5+
=SUBTITLE Meta role that adds support for multiple inheritance
6+
7+
role Metamodel::MultipleInheritance { ... }
8+
9+
Classes, roles and grammars can have parent classes, that is, classes to which
10+
method lookups fall back to, and to whose type the child class conforms to.
11+
12+
This role implements the capability of having zero, one or more parent (or
13+
I<super>) classes.
14+
15+
In addition, it supports the notion of I<hidden> classes, whose methods are
16+
excluded from the normal dispatching chain, so that for example C<nextsame>
17+
ignores it.
18+
19+
This can come in two flavors: methods from a class marked as C<is hidden>
20+
are generally excluded from dispatching chains, and C<class A hides B> adds
21+
C<B> as a parent class to C<A>, but hides it from the method resolution order,
22+
so that L<mro_unhidden|/type/Metamodel::C3MRO#method mro_unhidden> skips it.
23+
24+
=head1 Methods
25+
26+
=head2 method add_parent
27+
28+
method add_parent(Metamodel::MultipleInheritance:D: $Obj, $parent, :$hides)
29+
30+
Adds C<$parent> as a parent type. If C<$hides> is set to a true value, the
31+
parent type is added as a hidden parent.
32+
33+
=head2 method parents
34+
35+
method parents(Metamodel::MultipleInheritance:D: $obj, :$all, :$tree)
36+
37+
Returns the list of parent classes. By default it stops at L<Cool>, L<Any> or
38+
L<Mu>, which you can suppress by supplying the C<:all> adverb. With C<:tree>,
39+
a nested list is returned.
40+
41+
class D { };
42+
class C1 is D { };
43+
class C2 is D { };
44+
class B is C1 is C2 { };
45+
class A is B { };
46+
47+
say A.^parents(:all).perl; # (B, C1, C2, D, Any, Mu)
48+
say A.^parents(:all, :tree).perl;
49+
# ([B, [C1, [D, [Any, [Mu]]]], [C2, [D, [Any, [Mu]]]]],)
50+
51+
=head2 method hides
52+
53+
method hides(Metamodel::MultipleInheritance:D: $obj)
54+
55+
Returns a list of all hidden parent classes.
56+
57+
=head2 method hidden
58+
59+
method hidden(Metamodel::MultipleInheritance:D: $obj)
60+
61+
Returns a true value if (and only if) the class is marked C<is hidden>.
62+
63+
=head2 method set_hidden
64+
65+
method set_hidden(Metamodel::MultipleInheritance:D: $obj)
66+
67+
Marks the type as hidden.
68+
69+
=end pod

0 commit comments

Comments
 (0)