Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re^5: Factory Pattern in Perl6

by raiph (Deacon)
on May 19, 2016 at 21:14 UTC ( [id://1163535]=note: print w/replies, xml ) Need Help??


in reply to Re^4: Factory Pattern in Perl6
in thread Factory Pattern in Perl6

Thanks for the code. Fwiw I found it very readable.


Mu's BUILDALL returns the constructed object (per the self line at the end). I imagine a custom BUILDALL needs to do the same thing.

If you have the patience I'd appreciate you confirming that adding a trailing self line to Ack's BUILDALL fixes the problem you first posted about. (If so I'll update my original response to make it clear my guess about public attributes was a red herring.)

It's a mystery to me why converting a role to a class helped.


To wrap up I'll try to explain the error you just quoted. I'll go in to far more detail than I imagine you need in the hope it's helpful to someone some day.

I would appreciate confirmation that this error now makes sense to you as follows:

  1. Ack's BUILDALL returns Nil

    A block of code in Perl 6 is interpreted as a semilist (semicolon separated list) of statements. When a block is evaluated it returns the value of its last statement unless the code explicitly specifies otherwise. In this case the last statement is a for loop:

    unit class Game::Tetrachromat::Packet::Ack; # FTFY :) ... method BUILDALL(|) { ... for ... { ... } }

    A loop construct as the last statement is evaluated in sink context. Sink context means any value returned by an expression or statement gets thrown away -- down the sink as it were.

    Perl 6 uses Nil to communicate nothingness. So Ack's BUILDALL attempts to return Nil.

    Because Ack's BUILDALL's signature doesn't constrain what it returns, returning a Nil is fine and doesn't trigger a type error.

    The code continues to run...

  2. The Nil becomes an Any

    When a Scalar container is empty it pretends it contains a value, by default, an undefined Any. So if Nil gets assigned to any Scalar container with the default default the effect is indistinguishable from assigning it an undefined Any.

    Somewhere between the return of Ack's BUILDALL and the completion of the PacketFactory read_buffer method code my $packet = $packet_class.new... the Nil becomes an Any. The my $packet declaration doesn't specify a type constraint. So the assignment of an Any (or a Nil which becomes an Any) proceeds without a type error.

    The code continues to run...

  3. return $packet triggers a type error

    Rakudo finally gets around to reporting a type error at line 79 of PacketFactory because the enclosing method, read_buffer, defines a return type in its signature: returns Game::Tetrachromat::Packet but $packet contains an Any.

Replies are listed 'Best First'.
Re^6: Factory Pattern in Perl6
by hardburn (Abbot) on May 20, 2016 at 20:31 UTC

    Thanks--returning self at the end does fix that branch of the code. As for why converting away from a role helped, doing that was also meant using BUILD rather than BUILDALL; apparently, you don't have to return the object at the end of BUILD.

    It doesn't seem clear from the documentation that returning from BUILDALL would matter. Of these spots:

    https://doc.perl6.org/language/objects#Object_Construction

    https://doc.perl6.org/language/traps#BUILD_prevents_automatic_attribute_initialization_from_constructor_arguments

    None of them talk about the return value of BUILDALL, nor is it shown in the example code.

    Frankly, the perl6 docs seem to spend a lot of effort on showing how clever the language can be, and not enough on explaining what's going on.


    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      Thanks for verifying the `self` fix.

      Thanks for the feedback about the BUILDALL examples. I've fixed the two documents to show and comment on the returning of `self` and to switch `nextsame` to `callsame`.

      Thanks for working through this. Hopefully the fun aspects of Perl 6 resonate more than the frustrations.

        Thanks for all your help. I do want to see perl6 take off. Just need to file off some of the rough edges :)


        "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1163535]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-03-28 10:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found