Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re^2: Assignable Subroutines

by Aristotle (Chancellor)
on Jan 26, 2005 at 02:33 UTC ( [id://425100]=note: print w/replies, xml ) Need Help??


in reply to Re: Assignable Subroutines
in thread Assignable Subroutines

Btw, such a mechanism exists. You're basically saying that lvalue subs could be implemented as coroutines which are expected to yield the lvalue.

That's an interesting way of looking at this.

Makeshifts last the longest.

Replies are listed 'Best First'.
Re^3: Assignable Subroutines
by BrowserUk (Patriarch) on Jan 26, 2005 at 11:10 UTC
    ...implemented as coroutines which are expected to yield the lvalue.

    Not really. I may appear that way, but coroutines flip back and forth. This doesn't.

    In actuality, there would be very little difference between this and using a tie/STORE method to validate.

    • The method is called to obtain the lvalue which is returned to the calling code.
    • At some point the calling code makes an assignment to the lvalue and (because that lvalue is tied) the STORE method is called.

    Except in this case the "STORE method" is the code inline from the LVALUE statement to the end of method.

    One distinct difference between this and a tie/STORE method, is that if the lvalue in question is an array or a slice of an array, the "callback code" would only be called once. Not once for every value in the list being assigned.

    I am still unsure what happens in the calling code if the STORE method (disguised or not) rejects the attempt to store a value?

    I also thought about whether the PRE/POST or ENTER/LEAVE blocks associable with a method could be used to provide for a single place in which all validation is done, but my reading of the Apos & Syns leaves me very confused about when these get called, if ever, with an lvalue sub.

    Of course, this whole discussion is a waste of time. Who are we to question whether there is a better way? Explain to plebians. Pah!


    Examine what is said, not who speaks.
    Silence betokens consent.
    Love the truth but pardon error.

      I may appear that way, but coroutines flip back and forth.

      You don't appear that way, don't worry. ;-) Well, this feature could still be implemented with a coroutine.

      In any case, now I see what you were saying. There is indeed effectively no difference between your proposition and tieing. So why invent a new special-use mechanism rather than streamlining an existing one that can do the same job and more besides?

      Who are we to question whether there is a better way? Explain to plebians. Pah!

      Well, he did say:

      Attaching validation routines as setters is falling into a kind of cut-and-paste fallacy. Most such validation should really be done by the subtype system in Perl 6.

      And I agree with him. Just because we're used to putting validation in accessors doesn't mean it's the best way to do things. But all the discussion we've been having about lvalue subs revolves around trying to ease doing validation the way we've always done it, whether or not that's the best way to handle it. I've said before that I don't think it is.

      Makeshifts last the longest.

        Well, this feature could still be implemented with a coroutine.

        Agreed, it could. Indeed, that has some interesting possibilities for allowing the creation of tranparent coroutines.

        There is indeed effectively no difference between your proposition and tieing. So why invent a new special-use mechanism rather than streamlining an existing one that can do the same job and more besides?

        First, I would contend there is a significant difference. I have an attribute. I wish to control the values that can be assigned to that attribute (whether that attribute is externally exposed or only settable from with the class definition).

        The language allows me to have a method with the same name as that attribute. The obvious place to encapsulate all interactions with that attribute, is within that method. Scattering the decision as to what value will be set by assignments to the method, and the code to validate those assignments, into lots of little code blocks makes for complexity that should be avoided.

        Equally, if the lvalue in question is an array or hash, or a splice across one or the other, then recieving the values one at a time through the STORE method of a tied array makes for horrible problems.

        What if the validation criteria is that all the values in the list being assigned must be unique? Now I have to add additional (global) temporary variables in order that I can view the data as a single list in order to verify it. But how will I know when I have recieved the last value? And what happens if I reject the attempted list assignment? How does the recieving code get that notification? What does it do with it?

        Even if the assignment is a scalar lvalue. When the FETCH method is called, will it recieve the (variable number of?) parameters passed from the calling code that are required to determine the lvalue to use? (Think substr( $self, $offset, $length ) =  $str; here!)

        And if it does, and so the substring is assigned, now I want to validate that assignment. Will the same parameters as were passed to the FETCH be available to the STORE? If not, that again means that the programmer has to make provision for temporary (global) storage to retain those values between the calls to the FETCH and the STORE.

        The "example code" shown that outlines the inline tie mechanism shows a TEMP method. This is provided in order to allow lvalue subs to be temporised. What happens if the programmer doesn't provide a TEMP method? Does that mean that this method cannot be temporised? Or will the system provide a default TEMP method?

        Assuming the programmer does provide a TEMP method, what should it do? Provide a temporary lvalue that will not affect the actual attribute? Then if the TEMP value is never realised, then code will never call the STORE and that temporary value will what? When will the programmer know he can discard it?

        And if it is realised, when the STORE method is called, how will I know that I have to copy the value from the temporary lvalue I returned from the TEMP method ? In other words, how will the STORE method know when the value being STOREd was assigned to an lvalue provided via the FETCH method and when it has been assigned to an lvalue provided by the FETCH method? More global flags for communicating this information between these disparate methods?

        And I haven't even begun to consider the implications for multi-thread code where the FETCH and TEMP methods could be called several times before a STORE method is called. That would make the problem of trying to coordinate the cross-call, global metadata impossible.

        It may well be that all this has been thought about and resolved--but if it has, it certainly isn't obvious from the Apos and Syns.

        Most such validation should really be done by the subtype system in Perl 6.

        Anyone who remembers writing Pascal and having:

        TYPE ID = PACKED ARRAY [1..8] OF CHAR; TYPE FULLID = PACKED ARRAY [1..10] OF CHAR; TYPE NAME = PACKED ARRAY [1..32] OF CHAR; TYPE FIRSTNAME = PACKED ARRAY [1..12] OF CHAR; TYPE LASTNAME = PACKED ARRAY [1..20] OF CHAR;

        and all the problems that it created, will be twice shy of attempting to use a type system to provide for data validation. It is the first step on the slippery slope towards deeply hierarchical class structures, layered one atop the other. Something I thought Perl was explicitely trying to avoid.


        Examine what is said, not who speaks.
        Silence betokens consent.
        Love the truth but pardon error.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (8)
As of 2024-04-19 12:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found