Author: moritz Date: 2009-05-07 19:01:10 +0200 (Thu, 07 May 2009) New Revision: 26711
Modified: docs/Perl6/Spec/S07-iterators.pod docs/Perl6/Spec/S14-roles-and-parametric-types.pod docs/Perl6/Spec/S19-commandline.pod docs/Perl6/Spec/S21-calling-foreign-code.pod docs/Perl6/Spec/S28-special-names.pod docs/Perl6/Spec/S31-pragmatic-modules.pod Log: [spec] some EOL conversion; set SVN props to avoid that in future Property changes on: docs/Perl6/Spec/S07-iterators.pod ___________________________________________________________________ Added: svn:mime-type + text/plain; charset=UTF-8 Added: svn:eol-style + native Modified: docs/Perl6/Spec/S14-roles-and-parametric-types.pod =================================================================== --- docs/Perl6/Spec/S14-roles-and-parametric-types.pod 2009-05-07 08:50:06 UTC (rev 26710) +++ docs/Perl6/Spec/S14-roles-and-parametric-types.pod 2009-05-07 17:01:10 UTC (rev 26711) @@ -155,9 +155,9 @@ role Pet { is Friend; } - -=head2 Compile-time Composition +=head2 Compile-time Composition + A class incorporates a role with the verb "does", like this: class Dog is Mammal does Pet does Sentry {...} @@ -207,9 +207,9 @@ The proto method will be called if the multi fails: proto method shake { warn "They couldn't decide" } - -=head2 Run-time Mixins +=head2 Run-time Mixins + Run-time mixins are done with C<does> and C<but>. The C<does> binary operator is a mutator that derives a new anonymous class (if necessary) and binds the object to it: @@ -255,91 +255,91 @@ $fido does Wag($tail); $line does taint($istainted); - -Note that the parenthesized form is I<not> a subroutine or method call. -It's just special initializing syntax for roles that contain a single -property. +Note that the parenthesized form is I<not> a subroutine or method call. +It's just special initializing syntax for roles that contain a single +property. + The supplied initializer will be coerced to the type of the attribute. Note that this initializer is in addition to any parametric type supplied in square brackets, which is considered part of the actual type name: $myobj does Array[Int](@initial) - -A property is defined by a role like this: - - role answer { - has Int $.answer is rw = 1; - } - -The property can then be mixed in or, alternatively, applied using the -C<but> operator. C<but> is like C<does>, but creates a copy and mixes into -that instead, leaving the original unmodified. Thus: - - $a = 0 but answer(42) - -Really means something like: - - $a = ($anonymous = 0) does answer(42); - -Which really means: - - (($anonymous = 0) does answer).answer = 42; - $a = $anonymous; - -Which is why there's a C<but> operator. - -If you put something that is not a role on the right hand side of the -C<does> or C<but> operators then an anonymous role will be auto-generated -containing a single method that returns that value. The name of the method -is determined by doing .WHAT.perl on the value supplied on the RHS. The -generated role is then mixed in to the object. For example: - - $x does 42 - -Is equivalent to: - - $x does role { method Int() { return 42 } } - -Note that the role has no attributes and thus no storage; if you want that, -then you should instead use: - - $x does Int(42) - -Which mixes in the Int role and initializes the single storage location Int -that it declares with 42, and provides an lvalue accessor. -Note that .WHAT on an enumeration value stringifies to the name of the +A property is defined by a role like this: + + role answer { + has Int $.answer is rw = 1; + } + +The property can then be mixed in or, alternatively, applied using the +C<but> operator. C<but> is like C<does>, but creates a copy and mixes into +that instead, leaving the original unmodified. Thus: + + $a = 0 but answer(42) + +Really means something like: + + $a = ($anonymous = 0) does answer(42); + +Which really means: + + (($anonymous = 0) does answer).answer = 42; + $a = $anonymous; + +Which is why there's a C<but> operator. + +If you put something that is not a role on the right hand side of the +C<does> or C<but> operators then an anonymous role will be auto-generated +containing a single method that returns that value. The name of the method +is determined by doing .WHAT.perl on the value supplied on the RHS. The +generated role is then mixed in to the object. For example: + + $x does 42 + +Is equivalent to: + + $x does role { method Int() { return 42 } } + +Note that the role has no attributes and thus no storage; if you want that, +then you should instead use: + + $x does Int(42) + +Which mixes in the Int role and initializes the single storage location Int +that it declares with 42, and provides an lvalue accessor. + +Note that .WHAT on an enumeration value stringifies to the name of the enumeration, and as a result: 0 but True -Is equivalent to: - +Is equivalent to: + 0 but role { method Bool() { return True } } - -And thus the resulting value will be considered true in boolean context. - -The list syntax for composing multiple roles in a single C<does> or C<but> -by putting them in a list also applies here. Thus: - - 42 but ("the answer", False) - -Is equivalent to: - - 42 but (role { method Str() { return "the answer" } }, - role { method Bool() { return False } }) - -Which gives you a compact way to build context-sensitive return values. -Note that multiple roles rather than a single one are generated, so that -anything like: - - 42 but (True, False) - -Will fail as a result of standard role composition semantics (because two -roles are both trying to provide a method Bool). +And thus the resulting value will be considered true in boolean context. + +The list syntax for composing multiple roles in a single C<does> or C<but> +by putting them in a list also applies here. Thus: + + 42 but ("the answer", False) + +Is equivalent to: + + 42 but (role { method Str() { return "the answer" } }, + role { method Bool() { return False } }) + +Which gives you a compact way to build context-sensitive return values. +Note that multiple roles rather than a single one are generated, so that +anything like: + + 42 but (True, False) + +Will fail as a result of standard role composition semantics (because two +roles are both trying to provide a method Bool). + =head1 Traits Traits are just properties (roles) applied to declared items like Property changes on: docs/Perl6/Spec/S14-roles-and-parametric-types.pod ___________________________________________________________________ Added: svn:mime-type + text/plain; charset=UTF-8 Added: svn:eol-style + native Property changes on: docs/Perl6/Spec/S19-commandline.pod ___________________________________________________________________ Added: svn:mime-type + text/plain; charset=UTF-8 Added: svn:eol-style + native Property changes on: docs/Perl6/Spec/S21-calling-foreign-code.pod ___________________________________________________________________ Added: svn:mime-type + text/plain; charset=UTF-8 Added: svn:eol-style + native Property changes on: docs/Perl6/Spec/S28-special-names.pod ___________________________________________________________________ Added: svn:mime-type + text/plain; charset=UTF-8 Added: svn:eol-style + native Property changes on: docs/Perl6/Spec/S31-pragmatic-modules.pod ___________________________________________________________________ Added: svn:mime-type + text/plain; charset=UTF-8 Added: svn:eol-style + native