On 10/09/2017 20:35, li...@rhsoft.net wrote:
the whole switch discussion is broken by design
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.

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).


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.

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;
}

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

Regards,

--
Rowan Collins
[IMSoP]


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

Reply via email to