Re: Re: RFC Proposal: New assign value operator

From: Date: Thu, 19 Dec 2013 09:03:01 +0000
Subject: Re: Re: RFC Proposal: New assign value operator
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Thu, 19 Dec 2013 00:32:14 +0400, Tom Oram <[email protected]> wrote:

Sorry for the delay, I've been very busy. Before I let this go completely I just want to give a couple of examples. I get the feeling I'm not going to get anywhere with this but let me quickly just show a few things where I think this would make life easier: POINT 1 (mixing types including scalars): ------------- Consider: $emails = array(
    '[email protected]',
    new Email('[email protected]'),
    new AdvancedEmail('[email protected]'),
); Now which of these is a nicer way to clear all the addresses? foreach ($emails as &$email) {
    if ($email instanceof Email) {
        $email->setValue('');
    } elseif ($email instanceof AdvancedEmail) {
        $email->setAddress('');
    } else {
        $email = '';
    }
} OR foreach ($emails as &$emal) {
    $email := '';
} POINT 2 (protecting against changing types): ------------- Consider a library provides a base class called Product like so class Product {
    protected $price = 0.0;
} Next up someone extends this class in their application class SpecialProduct {
    /** @var float $price */
    public function specialSetPrice($price)
    {
        $this->doSomethingSpecial();
        $this->price = $price;
    }
} Now the original library maintainers decide to use a value object for the price changing Product to the following class Product {
    protected $price;
    public function __construct()
    {
        $this->price = new MoneyValue(0.0);
    }
} At this point the original app developpers would have to update their SpecialProduct so that $this->price = $price; becomes $this->price = new MoneyValue($price); HOWEVER if specialSetPrice had used $this->price := $price; from the start it would have adapted to any changes in type automatically and therefore no modification would have had to be made. In fact the child class didn't require any knowledge of the type defined in the parent. The type is set once then used with not requirement to maintain consistency. POINT 3 (it's nicer to use): ------------- I'm sorry but if you have a String class which you're using all over the place $var := 'new value'; is so much more readable (to me at least) than $var->setValue('new value'); To those that argue it's confusing with =, it's not, it's a different operator it's :=, just like +=, if you come across a new operator in the code which you havent seen before you got and look it up and find out what it does, just like if you see ** for the first time if the pow() RFC goes through. And again if the designers decide to stop using a scalar and use an object instead at some point all the assignments in the code won't have to be fixed!
I don't like that your examples embrace mutability for objects which clearly are immutable.

Thread (19 messages)

« previous php.internals (#70747) next »