Skip to content

Commit 7bfc6a8

Browse files
committed
BUILDALL has basically become an implementation detail
So remove it from the documentation. If you really want to have your own BUILDALL, you should *really*, **really** know what you're doing (and know what you'll be throwing away in terms of optimizations). So BUILDALL should be more in a "guts" type of documentation, if any. Please revert if consensus is that BUILDALL should stay documented and recommended as a way of unfluencing object creation.
1 parent fe6c917 commit 7bfc6a8

File tree

5 files changed

+19
-62
lines changed

5 files changed

+19
-62
lines changed

doc/Language/classtut.pod6

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ $eat.perform();
8787
=end code
8888
8989
In this case, C<BUILD> is needed since we have overridden the default
90-
new. C<bless> is eventually (after calling C<BUILDALL>) invoking it with
90+
new. C<bless> is eventually invoking it with
9191
the two named arguments that correspond to the two properties without a
9292
default value. With its signature, C<BUILD> converts the two positionals
9393
to the two attributes, C<&!callback> and C<@!dependencies>, and returns

doc/Language/objects.pod6

Lines changed: 10 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -418,9 +418,9 @@ using C<self>, not the shortcut.
418418
Note that if the relevant methods C<bless>, C<CREATE> of L<Mu>
419419
are not overloaded, C<self> will point to the type object in those methods.
420420
421-
On the other hand, C<BUILDALL> and the submethods C<BUILD> and C<TWEAK> are
422-
called on instances, in different stages of initialization. In the latter two
423-
submethods, submethods of the same name from subclasses have not yet run, so
421+
On the other hand, the submethods C<BUILD> and C<TWEAK> are
422+
called on instances, in different stages of initialization. Submethods of the
423+
same name from subclasses have not yet run, so
424424
you should not rely on potentially virtual method calls inside these methods.
425425
426426
=head2 Private methods
@@ -524,7 +524,7 @@ declared type:
524524
=end code
525525
526526
X<|new (method)>
527-
=head2 X<Object construction|BUILDALL (method)>
527+
=head2 Object construction
528528
529529
Objects are generally created through method calls, either on the type
530530
object or on another object of the same type.
@@ -544,8 +544,8 @@ L<arguments|/language/functions#Arguments> and uses them to initialize public at
544544
# OUTPUT: «y: 2␤»
545545
546546
C<Mu.new> calls method L<bless> on its invocant, passing all the named
547-
L<arguments|/language/functions#Arguments>. C<bless> creates the new object and then calls method C<BUILDALL>
548-
on it. C<BUILDALL> walks all subclasses in reverse method resolution order
547+
L<arguments|/language/functions#Arguments>. C<bless> creates the new object,
548+
and then walks all subclasses in reverse method resolution order
549549
(i.e. from L<Mu> to most derived classes) and in each class checks for the
550550
existence of a method named C<BUILD>. If the method exists, the method is
551551
called with all the named arguments from the C<new> method. If not, the public
@@ -558,10 +558,11 @@ After the C<BUILD> methods have been called, methods named C<TWEAK> are
558558
called, if they exist, again with all the named arguments that were passed
559559
to C<new>. See an example of its use below.
560560
561-
Due to the default behavior of C<BUILDALL> and C<BUILD>
562-
submethods, named arguments to the constructor C<new> derived from C<Mu> can
561+
Due to the default behavior of C<BUILD> annd C<TWEAK> submethods, named
562+
arguments to the constructor C<new> derived from C<Mu> can
563563
correspond directly to public attributes of any of the classes in the method
564-
resolution order, or to any named parameter of any C<BUILD> submethod.
564+
resolution order, or to any named parameter of any C<BUILD> or C<TWEAK>
565+
submethod.
565566
566567
This object construction scheme has several implications for customized
567568
constructors. First, custom C<BUILD> methods should always be submethods,
@@ -621,24 +622,6 @@ merely a common convention, one that is followed quite thoroughly in L<most Perl
621622
classes|/routine/new>. You can call C<bless> from any method at all, or use
622623
C<CREATE> to fiddle around with low-level workings.
623624
624-
Another pattern of hooking into object construction is by writing your own
625-
C<BUILDALL> method. To make sure that initialization of superclasses works fine,
626-
you need to C<callsame> to invoke the parent classes C<BUILDALL>.
627-
628-
=begin code
629-
class MyClass {
630-
method BUILDALL(|) {
631-
# initial things here
632-
633-
callsame; # call the parent classes (or default) BUILDALL
634-
635-
# you can do final checks here.
636-
637-
self # return the fully built object
638-
}
639-
}
640-
=end code
641-
642625
The C<TWEAK> submethod allows you to check things or modify attributes after
643626
object construction:
644627

doc/Language/traps.pod6

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -259,31 +259,6 @@ submethod BUILD(:$!x) {
259259
$!y = 18;
260260
}
261261
262-
Another, more general approach is to leave C<BUILD> alone, and hook into
263-
the C<BUILDALL> mechanism instead:
264-
265-
=begin code
266-
use v6.c;
267-
class A {
268-
has $.x;
269-
has $.y;
270-
method BUILDALL(|c) {
271-
callsame;
272-
$!y = 18;
273-
self
274-
}
275-
}
276-
277-
say A.new(x => 42).x; # OUTPUT: «42␤»
278-
=end code
279-
280-
Remember that C<BUILDALL> is a method, not a submethod. That's because
281-
by default, there is only one such method per class hierarchy, whereas
282-
C<BUILD> is explicitly called per class. That is the reason why, in
283-
order to properly initialize parent objects, it is required to use
284-
C<callsame> inside C<BUILDALL>, but not inside C<BUILD> (for more on the
285-
subject see L<object creation|/language/objects#Object_construction>).
286-
287262
=head1 Whitespace
288263
289264
=head2 Whitespace in regexes does not match literally

doc/Language/typesystem.pod6

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,10 +237,10 @@ class C {
237237
238238
=head4 Submethod BUILD
239239
240-
The L<submethod|/type/Submethod> C<BUILD> is called by C<.BUILDALL> defined in
241-
L<Mu|/type/Mu>, which in turn is called by L<.bless|/type/Mu#method_bless>. It
240+
The L<submethod|/type/Submethod> C<BUILD> is (indirectly) called by
241+
L<.bless|/type/Mu#method_bless>. It
242242
is meant to set private and public attributes of a class and receives all names
243-
attributes passed into C<.bless>. Since it is called by C<BUILDALL> the default
243+
attributes passed into C<.bless>. The default
244244
constructor L<.new|/type/Mu#method_new> defined in C<Mu> is the method that
245245
invokes it. Given that public accessor methods are not available in C<BUILD>,
246246
you must use private attribute notation instead.

doc/Type/Mu.pod6

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,8 @@ for more information.
271271
Low-level object construction method, usually called from within C<new>,
272272
implicitly from the default constructor, or explicitly if you create your own
273273
constructor. C<bless> creates a new object of the same type as the invocant,
274-
using the named arguments to initialize attributes by calling C<BUILDALL>) and
275-
returns the created object.
274+
using the named arguments to initialize attributes and returns the created
275+
object.
276276
277277
It is usually invoked within custom C<new> method implementations:
278278
@@ -299,7 +299,7 @@ disabled, since it can make it harder to correctly initialize the class from a
299299
subclass. For instance, in the above example, the C<new> implementation takes
300300
two positional arguments that must be passed from the subclass to the superclass
301301
in the exact order. That is not a real problem if it's documented, but take
302-
into account C<bless>, through C<BUILDALL>, will eventually be calling C<BUILD>
302+
into account C<bless> will eventually be calling C<BUILD>
303303
in the class that is being instantiated. This might result in some unwanted
304304
problems, like having to create a C<BUILD> submethod to serve it correctly:
305305
@@ -337,8 +337,7 @@ and thus serve that C<new> constructor, which can be called on C<Point-with-ID>
337337
since it is a subclass.
338338
339339
We might have to use something similar if we want to instantiate superclasses.
340-
C<bless> will help us with that, since it is calling the default C<BUILDALL>
341-
across all the hierarchy:
340+
C<bless> will help us with that, since it is calling across all the hierarchy:
342341
343342
=begin code
344343
class Str-with-ID is Str {
@@ -366,7 +365,7 @@ say "$ided-str, {$ided-str.^name}, {$ided-str.ID}"; # OUTPUT: «3,4, Str-with-ID
366365
We are *enriching* C<Str> with an auto-incrementing ID. We create a C<new> since
367366
we want to initialize it with a string and, besides, we need to instantiate the
368367
superclass. We do so using C<bless> from within C<new>. C<bless> is going to
369-
call BUILDALL, which will call C<Str.BUILD>. It will *capture* the value it's
368+
call C<Str.BUILD>. It will *capture* the value it's
370369
looking for, the pair C<value => $str> and initialize itself. But we have to
371370
initialize also the properties of the subclass, which is why within C<BUILD> we
372371
use the previously explained method to initialize C<$.ID> with the value that is

0 commit comments

Comments
 (0)