Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^8: Experimenting with Lvalue Subs (trade-offs)

by tye (Cardinal)
on Jan 25, 2005 at 09:53 UTC ( #424813=note: print w/ replies, xml ) Need Help??


in reply to Re^7: Experimenting with Lvalue Subs (trade-offs)
in thread Experimenting with Lvalue Subs

Yes, for the rare cases, it increases the number of calls. For the common cases, it can reduce the number of calls and complexity.

But this brings me back to my original suggestion, which was to leave :lvalue alone and provide :sassign (s=scalar) and :aassign (a=array since both 'list' and 'left' start with 'l') alternatives that make the easy stuff easy (and faster) but may not support the rare stuff (and, obviously, wouldn't support flipping between scalar and list contexts at run time).

Though you could still support general lvalue emulation for :sassign. For example, when an :sassign is used in an lvalue context other than a simple assignment (and perhaps a few other optimized cases), create a single closure that basicly curries the 'get' part of the method call and give this closure magic which means that assignments to it call it with the value to be assigned as the only parameter.1

So authors can choose to write 'set' routines in one piece instead of two while allowing their users to use assignment statements while Perl can automatically create a light-weight 'tied' closure only if the user uses the lvalue in a more complicated fashion.

But those who prefer to write 'set' methods in two pieces can optimize for the complex cases using :lvalue.

- tye        

1 I don't know if such makes sense for :aassign cases in Perl6 (it wouldn't in Perl5 because lists get flattened and supporting flattening of :aassign looks like a mine field to me).


Comment on Re^8: Experimenting with Lvalue Subs (trade-offs)
Re^9: Experimenting with Lvalue Subs (trade-offs)
by fergal (Chaplain) on Jan 25, 2005 at 16:28 UTC

    Actually I think your :sassign idea is implementable now using attributes. When you hit a sub with that attr you just need to whip it out and replace it with a sub that returns a tie which calls the original with the extra lvalue argument. Obviously you'd not be happy with the implementation but it would be worth doing just to be able to play with your sassign idea and see if you like it.

    Yes, for the rare cases, it increases the number of calls. For the common cases, it can reduce the number of calls and complexity.
    Increasing the number of calls is not just an efficiency issue, it's potentially a correctness issue, calling twice when previously we called once could do something nasty.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (7)
As of 2014-11-28 06:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (193 votes), past polls