Drupal 8 accomplishes this through assert() and a helper class.

function foo ( array $a ) {
  assert( Inspector::assertAllStrings( $a ));
}

This could be improved by having an collectionof operator similar to the
instanceof operator.

function ( array $a ) {
  assert( $a collectionof string );
}

I say "collectionof" because while arrays are the most common traversable
objects, they aren't the only ones.  The above approach, combined with the
existing assert structure, can provide the dev time checking of the code
while in under development, and it can be turned off in production. Or it
can be used outside of assert to be checked at all times.

Since this invokes a new keyword I think that would mean this solution
would be PHP 8.

Brackets might be used with instance of to notify it to traverse down one
level maybe??

function ( array $a ) {
  assert( $a instanceof [string] );
}

This avoids any BC issues, but it looks odd.  Not as odd as \ for a
namespace operation :P  But odd.


On Wed, Nov 1, 2017 at 10:40 AM, Rowan Collins <rowan.coll...@gmail.com>
wrote:

> On 31 October 2017 17:37:17 GMT+00:00, Levi Morrison <le...@php.net>
> wrote:
> >  - Our current infrastructure requires us to check every element of
> >the array for conformance. While undesirable I believe this can be
> >optimized away in the future if we care enough, and therefore not a
> >show-stopper.
>
> Do you have a mechanism in mind for "optimising this away"? I think
> releasing the feature without any optimisation will just lead to painful
> slowdowns when people use it without realising the implications.
>
> As I said in my previous message, it seems to be a fundamental issue with
> PHP's current type declarations that they are a) runtime, and b) asserted
> at arbitrary points, not at assignment. So even if you optimise the check
> slightly, it's still a separate check every time you enter a new function,
> which puts a limit on optimisation.
>
> The only way I've thought of to improve things is to cache the result of
> previous type checks, but this comes with all the normal challenges of
> caching. For instance, all assignments into the array would need to
> invalidate these cached checks, or check the new value against each.
> References would be particularly tricky, as they were with the property
> type hint proposal.
>
> If we selectively invalidated cached checks, we'd be running specific type
> checks at every variable assignment, but not letting the user actually
> assert on those checks. At which point, should we approach from the other
> direction, and let the user declare variable types, rather than assert
> value types?
>
> This is kind of implied if we implement generics, too, since explicitly
> constructing an instance of list<int> is asking the runtime to check future
> assignments to members of that list. In effect, could declaring "int $foo =
> 42;" be considered equivalent to "$foo = new checkedScalar<int>(42);"?
>
> Regards,
>
> --
> Rowan Collins
> [IMSoP]
>
> --
> PHP Internals - PHP Runtime Development Mailing List
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>

Reply via email to