Created
October 5, 2015 12:13
-
-
Save auroraeosrose/df3976dfdd95e81a4cef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Eos\Datastructures\Immutable; | |
use Eos\Datastructures\Struct; | |
class Rectangle extends Struct implements Immutable { | |
public $x; | |
public $y; | |
public $height; | |
public $width; | |
} | |
$rect = new Rectangle(['x' => 5, 'y' => 6, 'height' => 56, 'width' => 19]); | |
$rect->x = 5; // will throw ImmutableException |
And I guess now I see the point of nullables, since without them a struct would only sort of function. Unless we allowed a property to have a default value defined, so even $r = Rectangle{}; would give a valid set of values. Honestly I think I prefer that to nullable, as nulls are nothing but a source of pain and suffering.
That is, you'd have:
struct Rectangle {
int $x = 0;
int $y = 0;
float $width = 0;
float $height = 0;
}
$r = Rectangle{};
print $r->x; // prints 0.
That way there's still never a null.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Presumably each with() call would return a new struct instance, but with PHP's copy-on-write the memory impact should be minimal. That's how PSR-7 works, and the initial benchmarks said it was cheap enough to do.