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

OO magic (at least for me)

15 views
Skip to first unread message

BÁRTHÁZI András

unread,
Jun 26, 2005, 7:55:03 AM6/26/05
to perl6-l...@perl.org
Hi,

I'm wondering, if it's possible with Perl 6 or not?

class MyClass {

method mymethod($par) {
say "mymethod called!";
}

}

class ExClass is MyClass {

mymethod(12);

}

#> pugs myprog
mymethod called!

I would like to use mymethod to add ExClass some methods, etc.

///

Just another problem, related to the above:

class MyClass {

method whenmother() {
say "MyClass is parent now!!!";
say "Her child name is: " ~ ????;
}

}

class Child is MyClass {
}

#> pugs myprog
MyClass is parent now!!!
Her child name is: Child

Bye,
Andras

Piers Cawley

unread,
Jun 26, 2005, 11:54:30 AM6/26/05
to BÁRTHÁZI András, perl6-l...@perl.org
BÁRTHÁZI András <and...@barthazi.hu> writes:

I'd like to hope so. Actually, I don't think that this *specific* functionality
should be in the core, but the ability to implement it (just needs a unified
notifcation scheme that gets tickled when new classes, methods, subs, packages,
etc, get added to the image -- more detailed behaviour is a SMOP).

Juerd

unread,
Jun 26, 2005, 1:44:58 PM6/26/05
to BÁRTHÁZI András, Piers Cawley, perl6-l...@perl.org
BÁRTHÁZI András skribis 2005-06-26 19:35 (+0200):
> method fun1() { fun2(); }
> method fun2() { say "fun2!"; }
> *** No compatible subroutine found: "&fun2"

fun2 is a method, not a sub. You need method syntax to call it:

./fun2;

> class MyMethod { method fun1() { fun2(); } sub fun2() { say "fun2!"; }
> } class Child is MyMethod { }
> Child.fun1();
> Sounds good. It seems to me, that I can call fun2() from inside
> MyMethod, from everywhere. OK, Child is MyMethod, so can I do it there
> too? No. :(

IIRC, that's what submethods are for. Submethods aren't inherited. It is
unclear to me whether subs are.

Two requests:

1. Please indent code and don't use cutting lines.

2. Please use visually more different names, fun1 and fun2 look a lot
alike. Consider foo and bar.


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

BÁRTHÁZI András

unread,
Jun 26, 2005, 1:35:41 PM6/26/05
to Piers Cawley, perl6-l...@perl.org
Hi!

I'm trying to answering my questions. Still interested in some official
answer. :)

--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---


class MyMethod {
method fun1() {
fun2();
}

method fun2() {
say "fun2!";
}
}

class Child is MyMethod {
}

Child.fun1();
--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---

*** No compatible subroutine found: "&fun2"

I'm wondering why, but maybe, it's OK.

--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---


class MyMethod {
method fun1() {
fun2();
}
sub fun2() {
say "fun2!";
}
}

class Child is MyMethod {
}

Child.fun1();
--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---

fun2!

Sounds good. It seems to me, that I can call fun2() from inside
MyMethod, from everywhere. OK, Child is MyMethod, so can I do it there
too? No. :(

--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---


class MyMethod {
method fun1() {
fun2();
}
sub fun2() {
say "fun2!";
}
}

class Child is MyMethod {
fun2();
}

Child.fun1();
--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---

*** No compatible subroutine found: "&fun2"

The problem is calling fun2() from Child's declaration. As I think, the
calling just happens at when interpreting the declaration of Child, but
the scope is not Child's scope. Why?

Let's try Child.fun2()!

--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---


class MyMethod {
method fun1() {
fun2();
}
sub fun2() {
say "fun2!";
}
}

class Child is MyMethod {
Child.fun2();
}

Child.fun1();
--- 8< --- 8< --- 8< --- 8< --- 8< --- 8< --- 8< ---

fun2!

Works well. Is it a not yet implemented feature in Pugs, or is by design?

Still don't know, how can a method/sub automagically called, when I
inherite a class.

I would like to use the syntax you can see in my example #1, but the
syntax of example #2 is still OK. I would like to use calling the
classes own methods/subs when declaring a child, and calling a
method/sub automatically, when I'm declaring a child.

Bye,
Andras

Juerd

unread,
Jun 26, 2005, 2:24:46 PM6/26/05
to BÁRTHÁZI András, Piers Cawley, perl6-l...@perl.org
BÁRTHÁZI András skribis 2005-06-26 20:07 (+0200):
> Hmm. It really works. :) I'm getting the idea, what's the difference
> between methods and subs. Anyway, my implementation is, that ./ means
> self's method - and the class is not an instance, so it has no self.

The invocant can be a class too.

> Do you mean, that submethods for class methods (I don't know, if is it
> the official name of the non instance methods)? I don't think so.

No.

BÁRTHÁZI András

unread,
Jun 26, 2005, 2:07:27 PM6/26/05
to Juerd, Piers Cawley, perl6-l...@perl.org
Hi,

>> method fun1() { fun2(); }
>> method fun2() { say "fun2!"; }
>>*** No compatible subroutine found: "&fun2"
>
> fun2 is a method, not a sub. You need method syntax to call it:
>
> ./fun2;

Hmm. It really works. :) I'm getting the idea, what's the difference

between methods and subs. Anyway, my implementation is, that ./ means
self's method - and the class is not an instance, so it has no self.

./fun2 still not working at the second class's declaration.

> IIRC, that's what submethods are for. Submethods aren't inherited. It is
> unclear to me whether subs are.

Do you mean, that submethods for class methods (I don't know, if is it

the official name of the non instance methods)? I don't think so.

Bye,
Andras

0 new messages