Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test every function for immutability #4

Open
meszaros-lajos-gyorgy opened this issue Apr 13, 2021 · 3 comments
Open

Test every function for immutability #4

meszaros-lajos-gyorgy opened this issue Apr 13, 2021 · 3 comments

Comments

@meszaros-lajos-gyorgy
Copy link
Owner

All functions need to be double checked to see if they are in fact really immutable and if not, then they should be updated.
This might be a good place to add unit tests.

For example the following code is NOT immutable:

<?php
$point2d = new stdClass();
$point2d->x = 10;
$point2d->y = 20;

$point3d = O::assoc('z', 30, $point2d);

echo '<pre>';
var_dump($point2d);

// $point2d also contains "z"
@meszaros-lajos-gyorgy
Copy link
Owner Author

meszaros-lajos-gyorgy commented May 17, 2021

Normal arrays are immutable

$a = [1, 2, 3];

function xxxx($x) {
	$x[0] = 10;
	return $x;
}

$b = xxxx($a);

var_dump($a);
echo '<br />';
var_dump($b);

// $a[0] == 1, $b[0] == 10

Changing array size is also immutable:

<?php

$a = [1, 2, 3];

function xxxx($x) {
	$x[] = 10;
	return $x;
}

$b = xxxx($a);

var_dump($a);
echo '<br />';
var_dump($b);

// count($a) == 3, count($b) == 4

@meszaros-lajos-gyorgy
Copy link
Owner Author

Associative arrays are also immutable

$a = ['a' => 1, 'b' => 2, 'c' => 3];

function xxxx($x) {
	$x['a'] = 10;
	return $x;
}

$b = xxxx($a);

var_dump($a);
echo '<br />';
var_dump($b);

// $a['a'] = 1, $b['a'] = 10

@meszaros-lajos-gyorgy
Copy link
Owner Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant