Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re^3: How will you use state declared variables in Perl6?

by jgallagher (Pilgrim)
on Jul 08, 2005 at 21:56 UTC ( #473579=note: print w/ replies, xml ) Need Help??


in reply to Re^2: How will you use state declared variables in Perl6?
in thread How will you use state declared variables in Perl6?

I believe BrowserUK's point is that if foo is just a global subroutine, there is no difference. On the other hand, suppose you had

package Bar; sub new { ... } sub foo { state $bar; $bar++; return $bar; }
Then later...
$bar1 = Bar->new(); $bar2 = Bar->new(); $bar1->foo(); # returns 1, as you would expect $bar1->foo(); # returns 2, as you would expect $bar2->foo(); # returns 3, possibly unexpected
At least this is my understanding. Is this what you meant, BrowserUK?


Comment on Re^3: How will you use state declared variables in Perl6?
Select or Download Code
Re^4: How will you use state declared variables in Perl6?
by Limbic~Region (Chancellor) on Jul 08, 2005 at 22:03 UTC
    jgallagher,
    BrowserUk's response boils down to "don't use state variables as a replacement for class variables when doing OO". I agree with that. The argument that it will be easy to make this mistake is false though as p6 OO looks visually a lot different from p5 (method foo() vs sub foo() for instance). I am not saying caution isn't warranted, but this is just once case that doesn't negate the usefulness elsewhere.

    Cheers - L~R

    Update: I reworded my response to be clearer but I believe the spirit of the message was left in tact.

Re^4: How will you use state declared variables in Perl6?
by BrowserUk (Pope) on Jul 08, 2005 at 22:38 UTC

    Yes. That is a good example of the possible surprise factor.

    Now inherit Class Qux from Class Bar, and every time you call ->foo() from any instance of either class, state $bar will be incremented. This has the potential for an even bigger surprise factor I think.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
      Nah, it's only surprising if foo doesn't know it's a method. Methods should never be surprised by being inherited. And Perl 6 methods always know they're methods because they have their own keyword. If foo doesn't properly manage its own state, that foo's problem, not anyone else's.

        Okay. How will a method know how to segregate the management of a state var under inheritance?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2014-12-28 16:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls