Author: lwall Date: 2009-09-07 19:53:06 +0200 (Mon, 07 Sep 2009) New Revision: 28199
Modified: docs/Perl6/Spec/S04-control.pod docs/Perl6/Spec/S10-packages.pod docs/Perl6/Spec/S12-objects.pod Log: [S10] specify time of execution of package blocks and module (non)mainline code [S12] give example of use of yada classes Modified: docs/Perl6/Spec/S04-control.pod =================================================================== --- docs/Perl6/Spec/S04-control.pod 2009-09-07 17:43:11 UTC (rev 28198) +++ docs/Perl6/Spec/S04-control.pod 2009-09-07 17:53:06 UTC (rev 28199) @@ -823,7 +823,7 @@ sets the topic in its signature can be broken out of. At run time, C<break> uses a control exception to scan up the dynamic chain to find the activation record belonging to that same outer block, and -when it has found that scope, it does a C<.leave> on it to unwinde +when it has found that scope, it does a C<.leave> on it to unwind the contexts. If any arguments are supplied to the C<break> function, they are passed out via the C<leave> method. Since leaving a block is considered a successful return, breaking out of one is also considered Modified: docs/Perl6/Spec/S10-packages.pod =================================================================== --- docs/Perl6/Spec/S10-packages.pod 2009-09-07 17:43:11 UTC (rev 28198) +++ docs/Perl6/Spec/S10-packages.pod 2009-09-07 17:53:06 UTC (rev 28199) @@ -13,8 +13,8 @@ Created: 27 Oct 2004 - Last Modified: 13 Feb 2009 - Version: 9 + Last Modified: 7 Sep 2009 + Version: 10 =head1 Overview @@ -85,6 +85,44 @@ syntax that lets you do a lookup in a particular symbol table. In this case, the key is not parsed for C<::>. It's just a hash lookup. +All package bodies (including module and class bodies) execute at the +normal execution time of the code in which they are embedded. For normal +mainline code, this is the normal flow of execution; if this is too late +to initialize something in the package that you want to be initialized, consider +use of a MAIN subroutine, which is invoked at the end of normal execution. +See L<S06/Declaring a C<MAIN> subroutine>. + +For packages (modules, classes, roles, etc.) defined in separate files +from the mainline code, there can be no mainline code by definition, +but the top-level code in the used module needs to be executed at +some point in case things need initialization. Invocation of this +pseudo-mainline code in the module notionally happens no later than at +the point of the C<use> or C<need> call in the process of compilation, +but the module's code is assumed to be sufficiently uninteresting that +it need be executed only once regardless of how many times the module +is used subsequently in the compilation. (In fact, it might not need +to run at all if the result of some previous compilation's run has +been cached.) + +If it is desired to have code that varies in meaning from run to run, +then you should put such code into an INIT block. (Likewise, you +could put code into a CHECK block that has inconsistent semantics +from compilation to compilation, but that's probably a bad idea.) + +In any case, it is erroneous for any external module to depend +on any knowledge of its user with respect to compilation order or +other contextual information, since other users may also depend on +this single "first-use" execution and expect consistent semantics. +(Really, all such contextual dependencies should be passed in at run +time to the routines or methods of your module as normal parameters or +contextual variables. For instance, you cannot know at module compile +time whether your caller is going to be using 'fatal' semantics or not. +That is dynamically scoped info.) + +If you wish to have a module that does something extra if invoked +standalone, define a MAIN subroutine, which will be ignored if +the module is merely used/needed elsewhere. + =head1 Package nesting A declaration of any object of the form C<A::B::c> also creates (if needed) Modified: docs/Perl6/Spec/S12-objects.pod =================================================================== --- docs/Perl6/Spec/S12-objects.pod 2009-09-07 17:43:11 UTC (rev 28198) +++ docs/Perl6/Spec/S12-objects.pod 2009-09-07 17:53:06 UTC (rev 28199) @@ -13,8 +13,8 @@ Created: 27 Oct 2004 - Last Modified: 29 Jun 2009 - Version: 86 + Last Modified: 7 Sep 2009 + Version: 87 =head1 Overview @@ -42,23 +42,43 @@ There are two basic class declaration syntaxes: class Foo; # rest of file is class definition - ... + has $.foo; - class Bar {...} # block is class definition + class Bar { has $.bar } # block is class definition The first form is allowed only as the first declaration in a compilation unit (that is, file or eval string). -In either case, the code represented by C<...> executes at compile -time as the body of a method of the metaclass, which is responsible -for interpreting the keywords of the class definition. (And since a -class is also a module, it also handles any module-oriented keywords. -You can export subs from a class at "use" time, for instance.) +If the class body consists of a single statement consisting of a +single C<< prefix:<...> >> (yada) listop, the class name is introduced +without a definition, and a second declaration of that class in the +same scope does not complain about redefinition. Thus you may +forward-declare your classes: -If the class body consists only of a literal C<...> (yada) term, it is -interpreted as a forward declaration that just tells the compiler that the -class name is a type name, to be defined later on. + class A {...} # introduce A as a class name without definition + class B {...} # introduce B as a class name without definition + my A $root .= new(:a(B)); + + class A { + has B $.a; + } + + class B { + has A $.b; + } + +As this example demonstrates, this allows for mutually recursive class +definitions (though, of course, it can't allow recursive inheritance). + +It is also possible to extend classes via the C<augment> declarator, +but that is considered somewhat antisocial and should not be used +for forward declarations. + +[Conjecture: we may also allow the C<proto> and C<multi> modifiers +to explicitly declare classes with multiple bodies participating in +a single definition intentionally.] + A named class declaration can occur as part of an expression, just like named subroutine declarations.