Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re^7: Experimenting with Lvalue Subs

by Juerd (Abbot)
on Jan 27, 2005 at 22:53 UTC ( #425803=note: print w/replies, xml ) Need Help??

in reply to Re^6: Experimenting with Lvalue Subs
in thread Experimenting with Lvalue Subs

If any of those take the value of the attribute outside it's defined range, then no, I don't want it to "Just work". I want it to "Just NOT work".

Most of the time, this isn't needed. A normal variable $foo can have any value and I don't understand why instance variables should be different in this.

Still, because value validation is a nice thing to have, tie with a STORE handler isn't a bad idea. But this shouldn't be specific to OO - the solution should apply to all variables. Our implementation, however, cheats by ignoring normal variables. This was easier :)

sub foo : Property { $_ < 10 } main->foo = 9; main->foo++; # dies

Perl 6 will provide a real solution, with a nice syntax even! In fact, more than one nice syntax!

has $foo where { $_ < 10 };
This extends to non-instance variables too. Just use "our" or "my" instead of "has".
my $foo where { $_ < 10 }; $foo = 9; $foo++; # dies

Juerd # { site => '', plp_site => '', do_not_use => 'spamtrap' }

Replies are listed 'Best First'.
Re^8: Experimenting with Lvalue Subs
by BrowserUk (Pope) on Jan 28, 2005 at 01:09 UTC

    Be warned. This reply drifts way off the topic you raised/responded to. Sorry! Feel free to ignore it--not that you need me to tell you that :)

    Most of the time, this isn't needed. A normal variable $foo can have any value and I don't understand why instance variables should be different in this.

    I'm not sure that I agree with that. If the value of $foo comes from "outside" my program, and the use to which it is put requires that it be positive, then I am going to validate that it is. And I am going to validate it just after it receives it's value from that external source.

    I would like that same ability for lvalue subs. I'm sticking with the use of that term for a reason. The lvalue sub, may be a method tied directly to an attribute. But it may also be a completely transient value untied(sic) to any permanent attribute of the class which it is a part of, and used to effect some change of state of the object without being a directly accessed attribute.

    I hate giving examples, especially when related to OO things, because they always sound contrived, but I'll do it anyway. The one that springs to mind relates to a real test set by the US Navy I was reading about recently.

    The object is an instance of a TrackedObject class in a radar detection system. At some point, the radar detects a new contact and creates an instance to track it. Some time later, an ident (FriendOrFoe) is received from the object, and it is assigned to the object:

    $trackedObject->Ident = $ident;

    The ident itself (some alphanumeric code), isn't stored in the object. It could change anytime and is pretty meaningless except for the significance it carries. The ident must be verified against the current database of valid ident code for this day, this location etc. That means calling another system to get that verified, but before doing so, I must at least verify that it is of the right format for a valid ident.

    In order to get the proper verification (which will be responsible for marking this currently unknown object as freind or foe), I must also pass the current datetime, and the location of the ship or aircraft that is detecting it, and the heading of the contact.

    ... method Ident: lvalue { LVALUE{ my $ident }; reject( $ident ), return unless $ident =~ $RE_IDENT{$LOCATION}{$DATETIME}; $self->FreindOrFoe = ValidateIdent( $ident, $self->currentHeading, $LOCATION, $DATETIME ); } ...

    Yes. It is contrived, but it demonstrates 3 things;

    1. The lvalue method need not be attached to a permenant attribute.
    2. The validation can be complex. More complex than can easily be accomodated by any generalised trait mechanism. It can be very specific to this particular method.
    3. It can require reference to instance specific information, and global state.

      All this is available, by definition has to be available, right there at the point of validation directly after the lvalue is assigned.

    And that's the cruz of my argument. Everything, whatever that may be, required for validating the value assigned has to be available at the point of assignment. So why try and move it elsewhere?

    I sit down to code to this object--I look up the "attributes" available and see the one I want to assign to. If I want to know what values I should be assigning, if the docs are vague or non-existant, I should be able to go to the definition of the method and read what validation is done.

    I simple do not understand why it would be put anywhere else--except if it is technically infeasible. It isn't, so why not put it in the (to me:) obvious place?

    I realise that this is more a case of syntax that semantics (or is that the other way around? :), and I realise that there is a move in P6 to make as much as possible declarative rather than imperative, but I don't believe that everything can be done declaratively.

    Actually, the more time I spend thinking about it, the less likely do I think that much of the declarative stuff in P6 will get a lot of use. It (IMO) requires a style of programming, and a way of thinking, that I do not see fits with the tradition of Perl.

    In many ways, it requires much deeper "systems design" than P5, and detracts from the immediacy and the "rapid prototyping" way of working that I think is Perl's greatest strength.

    Time will tell on that one I guess.

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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://425803]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2017-06-28 01:12 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (619 votes). Check out past polls.