Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

trying to use a role within a role

0 views
Skip to first unread message

Richard Hainsworth

unread,
Sep 30, 2006, 6:26:22 AM9/30/06
to perl6...@perl.org
Could someone please tell me what is wrong with the following?

role win_text {
has $.win_mytxt1 is rw;
has $.win_mytxt2 is rw;
};

role mywindow {
has $.border is rw;
has $.colour is rw;
does win_text;
};

my $w = new mywindow;

$w.border = 2;
$w.colour = 'red';
say $w.border;
say $w.colour;

$w.win_mytxt1 = 'quick red fox';
$w.win_mytxt2 = ' jumped over the lazy dog';
say $w.win_mytxt1 ~ $.win_mytxt2;

Running in a terminal yields:

$ pugs ./test.p6
2
red
*** No compatible subroutine found: "&win_mytxt1"
at ./test.p6 line 19, column 1-32
$

Hence defining attributes in a role and accessing them works as defined
in Synopsis 12. (If I understand the synopsis correctly, when I write
'my $w = new mywindow' I am autogenerating a class from a role.)
Otherwise I would not get the '2' and 'red' printed out.

But I could not find any mention that an attribute in a role called by a
role would have to be accessed differently.

Regards,
Richard

Audrey Tang

unread,
Oct 2, 2006, 5:56:37 AM10/2/06
to Richard Hainsworth, perl6...@perl.org

在 Sep 30, 2006 6:26 PM 時,Richard Hainsworth 寫到:

> role win_text {
> has $.win_mytxt1 is rw;
> has $.win_mytxt2 is rw;
> };
>
> role mywindow {
> has $.border is rw;
> has $.colour is rw;
> does win_text;
> };
>
> my $w = new mywindow;
>
> $w.border = 2;
> $w.colour = 'red';
> say $w.border;
> say $w.colour;
>
> $w.win_mytxt1 = 'quick red fox';
> $w.win_mytxt2 = ' jumped over the lazy dog';
> say $w.win_mytxt1 ~ $.win_mytxt2;

On the last line it should be $w.win_mytxt2, not $.win_mytxt2.

At this moment only "role mywindow does win_text" works; the
statement-level "does" form was not implemented, but I should be
able to do so in the next few days. I've sent you a Pugs commit bit;
can you check this test of yours in under t/oo/ somewhere?

Thanks!
Audrey

Audrey Tang

unread,
Oct 2, 2006, 8:53:59 AM10/2/06
to Audrey Tang, Richard Hainsworth, perl6...@perl.org

在 Oct 2, 2006 5:56 PM 時,Audrey Tang 寫到:

> At this moment only "role mywindow does win_text" works; the
> statement-level "does" form was not implemented, but I should be
> able to do so in the next few days.

Update: It's now implemented as r13782.

Richard: The t/README file should get you started at writing (and
committing) your first test; people on irc.freenode.net #perl6 will
definitely help as well.
Pugs operates under a seek-forgiveness-not-permission model, so it's
easiest to just copy a test in the same directory, modify it, and
check it in.
If anything went wrong, the gnomes on #perl6 will fix it right away. :-)

Thanks!
Audrey

0 new messages