You're right, struct isn't the right word - "value" is probably more
accurate.

value Color
{
    public $r = 1.0;
    public $g = 1.0;
    public $b = 1.0;

    public function __construct($r, $g, $b)
    {
        $this->r = $r;
        $this->g = $g;
        $this->b = $b;
    }

    public function mix(Color $other, $amount)
    {
        $this->r = $other->r * $amount + $this->r * (1 - $amount);
        $this->g = $other->g * $amount + $this->g * (1 - $amount);
        $this->b = $other->b * $amount + $this->b * (1 - $amount);

        return $this;
    }
}

$red = new Color(1, 0, 0);
$blue = new Color(0, 0, 1);
$mix = $red->mix($blue);

Okay, this would probably work - since $this actually refers to an array
inside the mix() method scope, the array would be copied as soon as the
first assignment is made... but I can see how this is starting to look
rather confusing, given that it clashes with existing syntax and how actual
objects currently work...

Probably not a good idea after all...

Though I still wish there was some better way to handle value-types...


On Thu, Apr 4, 2013 at 12:57 PM, Benjamin Eberlei <kont...@beberlei.de>wrote:

>
> structs as in c# don't have methods, however DateTime has them. so this
> doesn't work. What you can do is just pass all the data in the constructor
> and then don't change it, and you have your value type that is immutable.
> It just requires a bit self control.
>
>

Reply via email to