Am 10.09.2017 um 22:35 schrieb Rowan Collins:
On 10/09/2017 20:35, li...@rhsoft.net wrote:
with "<?php declare(strict_types=1);" the intention is pretty clear

strict_types does not currently have any effect on comparisons anywhere, and nor is it likely ever to do so. It is a very specific option, not a broad set of behaviours like "use strict" in JS or Perl.

in case of a normal comparison you have !== and === to make it explicit

raise an error if cases are mixed (case 'a' and case 1 under  the same switch)

It's interesting that the behaviour you think is obvious is completely different to what I think of: if the author of the code has put both case 42 and case '42' in the switch statement, the "strict" interpretation in my mind would be that both branches are reachable, as though tested with an "===" operator (whereas currently the second would never be selected, because no value =='42' that doesn't also ==42).

yes, for non-strict code that's fine

and also raise an error in a case like below when $a is  anything else then int

I guess this explains the different direction you're coming from: what you want could perhaps be termed a "strongly typed switch statement", where both input and case values are checked as soon as possible for being of some specified type.

yes, that's what i expect from declare(strict_types=1); wherever it is possible for several reasons

* as developer i like fail early and raise a clear exception
* in the best case even at compile time and not 3 days later at runtime
* it makes code often more secure over time when it points
  out missing input validation and from where a bad value comes
  instead burry a error where it triggers the exception
* it enables optimizations like 7.1 got a lot
* it even may help a future JIT to optimize

due change every code i wrote to strict-types and enhance every function to type-hints and return types i found a ton of hidden probably bugs and especially fuzzying points out code weakness

The logical syntax for that would probably be an explicit type in the switch statement somewhere, like this:

switch(int $a)
{
  case 1: ..; break;
  case 2: ..; break;
}

i like that!

Although an obvious question would be why switch gets such a version, and what other language constructs should follow suit.

well, you have to start somewhere and the start was done with 7.0 for function params and return types, switch is an obvious place that could be enhanced and the syntax above is 100% consistent to function params

it's not only consistent in the syntax, it's also consistent in behavior meaning in no-strict-mode make a implicit type cast and it's not a new syntax element

currently i have no other language constrcuts in mind but i think starting here could lead in people point out other places where it makes sense and since it's completly opt-in it don't break any existing code

ok, i have one in mind
foreach($arr as int $x)

raise a runtime error when you expect a array with only numbers and the comes '42' and in strict-mode do a consistent implicit cast in non-strict where you can be sure in both cases that you deal with a int within the loop

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to