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

Adding methods to (existing) classes

3 views
Skip to first unread message

Patrick R. Michaud

unread,
Jun 15, 2005, 4:38:33 PM6/15/05
to perl6-i...@perl.org
Given that I have a subroutine stored in a PMC, is there a way
for me to add that subroutine as a method to an existing class?

In other words, I know I can use <store_global> to add the subroutine
into the current namespace...

.namespace [ "Foo" ]

.sub main @MAIN
$S0 = ".sub h @ANON\n print \"Hello\"\n.end\n"
$P0 = compreg "PIR"
$P1 = compile $P0, $S0
store_global "Foo", "hello", $P1

"hello"()
.end

but is there a way for me to store such a subroutine as a method of an
existing class? I.e., I'd like to be able to do something along
the lines of:

.sub main @MAIN
$P99 = newclass "Foo"

$S0 = ".sub h @ANON\n print \"Hello\"\n.end\n"
$P0 = compreg "PIR"
$P1 = compile $P0, $S0
store_global "Foo", "hello", $P1

$I2 = findclass "Foo"
$P2 = new $I2
$P2."hello"()
.end

Am I totally off-the-wall here, or am I just overlooking a pragma, opcode,
or something similar that would allow this to work?

Pm

Leopold Toetsch

unread,
Jun 16, 2005, 4:50:37 AM6/16/05
to Patrick R. Michaud, perl6-i...@perl.org
Patrick R. Michaud wrote:

> ... I.e., I'd like to be able to do something along


> the lines of:
>
> .sub main @MAIN
> $P99 = newclass "Foo"
>
> $S0 = ".sub h @ANON\n print \"Hello\"\n.end\n"
> $P0 = compreg "PIR"
> $P1 = compile $P0, $S0
> store_global "Foo", "hello", $P1
>
> $I2 = findclass "Foo"
> $P2 = new $I2
> $P2."hello"()
> .end

That works fine if you s/findclass/find_type/

C<findclass> is one of the ugly/evil/wrong opcode warts that will be
removed soon. It duplicates the functionality of C<find_type> with
restricted usage (returns a *boolean*) and inconsistent syntax.

leo

Patrick R. Michaud

unread,
Jun 17, 2005, 3:12:47 PM6/17/05
to Leopold Toetsch, perl6-i...@perl.org
On Thu, Jun 16, 2005 at 10:50:37AM +0200, Leopold Toetsch wrote:
> Patrick R. Michaud wrote:
> >... I.e., I'd like to be able to do something along
> >the lines of:
> >[omitted]

> That works fine if you s/findclass/find_type/

Excellent! For some reason I thought it didn't work for me even
when I tried find_type, but it seems to be working now. This
simplifies things a lot -- thanks!

Pm

0 new messages