develooper Front page | perl.perl6.language | Postings from June 2005

Re: OO magic (at least for me)

Thread Previous | Thread Next
From:
=?ISO-8859-1?Q?B=C1RTH=C1ZI_Andr=E1s?=
Date:
June 26, 2005 10:36
Subject:
Re: OO magic (at least for me)
Message ID:
42BEE76D.4070804@barthazi.hu
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

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About