php-src icon indicating copy to clipboard operation
php-src copied to clipboard

Add boolean assignment operators

Open theking2 opened this issue 4 years ago • 9 comments

Description

Currently there are no ||= &&= operators. A bitwise &= |= do exist, but will change the type of the operand.

Use case in pseudo

function test(array $a): bool
{
    $result = true;
    foreach($a as $element) {
      $result &= $element-> test();
    }
    return $result;
}

theking2 avatar Apr 06 '22 12:04 theking2

See https://github.com/php/php-src/pull/4535 for one of the issues regarding these operators. I don't see a clear benefit to have these operators (and apparently, most other languages which have the logical operators and combined assignment operators do not either), so I strongly suggest to pursue the RFC process.

cmb69 avatar Apr 06 '22 14:04 cmb69

Well here is one : C#

theking2 avatar Apr 06 '22 15:04 theking2

Well here is one : C#

Nope. Note: The conditional logical operators && and || don't support compound assignment.

However, JS added that in ES2021 (w/ short-circuiting): https://github.com/tc39/proposal-logical-assignment https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Logical_OR_assignment https://2ality.com/2020/06/logical-assignment-operators.html

KapitanOczywisty avatar Apr 06 '22 19:04 KapitanOczywisty

I don't see a clear benefit to have these operators (and apparently, most other languages which have the logical operators and combined assignment operators do not either)

@cmb69 Beside shorter syntax, there are 2 more "benefits" from using a ||= b instead of a = a || b:

1. Setter is only called when not short-circuiting, this is similar to a || (a = b):
new class
{
	private array $data = ['foo' => true, 'bar' => false,];
	function __construct()
	{
		$this->foo ||= true;
		assert($this->data['foo'] === true);

		$this->bar ||= false;
		assert($this->data['bar'] === false);
	}
	function __get(string $name): bool
	{
		echo "Getter called for {$name}\n";
		return $this->data[$name];
	}
	function __set(string $name, bool $value): void
	{
		echo "Setter called for {$name}\n";
		$this->data[$name] = $value;
	}
};

should be expected:

Getter called for foo
Getter called for bar
Setter called for bar

https://3v4l.org/LJp09

Edit: 1. is not playing well with PHP

  1. it's easier to use with incrementing keys:
$array = [false, false, false];
$key = 0;

$array[$key++] ||= true;
$array[++$key] ||= true;

assert($array === [true, false, true]);

https://3v4l.org/FGBjj

KapitanOczywisty avatar Apr 07 '22 13:04 KapitanOczywisty

@KapitanOczywisty, that is actually what I find most confusing. Consider $a = 0; $a &&= $b. Would that result in $a === false? Analogous for $a = "foo"; $a ||= $b.

cmb69 avatar Apr 07 '22 14:04 cmb69

Probably should always convert to bool if not already. First feature is not that important for PHP, in JS setters are more common and || doesn't change type.

KapitanOczywisty avatar Apr 07 '22 14:04 KapitanOczywisty

The code above is sub-optimal anyway, since the loop should stop once $result is false.

function test(array $a): bool
{
    foreach($a as $element) {
      if (!$element-> test()) return false;
    }
    return true;
}

I find the coercion that @cmb69 mentioned confusing too.

iluuu1994 avatar Apr 15 '22 13:04 iluuu1994

A trivial example does have a trivial optimization. If test was doThing that had side effects and all methods need to be called then you can't optimize it away.

@KapitanOczywisty, that is actually what I find most confusing. Consider $a = 0; $a &&= $b. Would that result in $a === false? Analogous for $a = "foo"; $a ||= $b.

I'm going to have to think about this a bit but generally I don't know that any behavior is really confusing if you consider this to be a condensed version of $a = $a && $b and $a = $a || $b like other assignment operators. Setting aside how coercion can generally confuse things, the behavior for this seems well defined unless I'm missing part of your question.

What isn't immediately clear to me is

  1. would be affected by strict types? are other assignment operators?
  2. the discussion around short circuiting. Is that relevant, and what would it mean to PHP.

neclimdul avatar May 17 '22 18:05 neclimdul

There has not been any recent activity in this feature request. It will automatically be closed in 14 days if no further action is taken. Please see https://github.com/probot/stale#is-closing-stale-issues-really-a-good-idea to understand why we auto-close stale feature requests.

github-actions[bot] avatar Aug 16 '22 00:08 github-actions[bot]