Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:98930 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 32180 invoked from network); 2 May 2017 10:53:48 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 2 May 2017 10:53:48 -0000 Authentication-Results: pb1.pair.com header.from=giovanni.g@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=giovanni.g@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.217.182 as permitted sender) X-PHP-List-Original-Sender: giovanni.g@gmail.com X-Host-Fingerprint: 209.85.217.182 mail-ua0-f182.google.com Received: from [209.85.217.182] ([209.85.217.182:35688] helo=mail-ua0-f182.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 39/D3-02776-A3568095 for ; Tue, 02 May 2017 06:53:46 -0400 Received: by mail-ua0-f182.google.com with SMTP id e55so50235930uaa.2 for ; Tue, 02 May 2017 03:53:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:sender:in-reply-to:references:from:date:message-id :subject:to:cc; bh=vfJ7obuVsizAVZmYAlhpaqO1QNF2gK9zu7MCjJR7IGg=; b=lovFUSBRL65Eyt8rmmdn/uGqGIxWKEMN5DrrZs1uX9xLodi3R1t5fxmdIkXao9ZOSo CGLy1ujcy3FZTslXZdARR2oP7UKFtinNgLfGldwclPo/57G1iEzrkTfM4461LA8KgHMe D63K+0XdR9h09JlVv2EX6ljSQY7+Ii9s+AOoJjxFNinwZSMGjESUOKSC85l13F+Dz6Js 2DXwSqQA+Ojv+dvxRIX/UprBGSJywn17EZtFCXvnFrrS3K1dhMok+XAi4PaKZyWjngHa wLGFHl/Es7e2xEr+Wkn1xuoLnS8vJIcbsnDiYt/nZ5S1kSnE5EiUnz4CGZk/Tv2NKWX0 DpEA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:sender:in-reply-to:references:from :date:message-id:subject:to:cc; bh=vfJ7obuVsizAVZmYAlhpaqO1QNF2gK9zu7MCjJR7IGg=; b=bKTNHqX5UMTfV4L+Osd3ZtY8JSGNCiTFaOWKvEa/KOMQYUPPayC33hZgMgtMW8BNou XEekfXYns4XYbXglrztjKe+w/0xzWN2osVFJHYxbQzHlDK8+/8vGLqHBcN0HdanIsuSa R/9MD0CkOOtpJarIeHRqoOVEjJLzKk47bAsSqNNNMCCwuTtow4zc7Y6IedqQe0gC7HJf 03yVJSf8nJJmRpp6dvmjdQ4Jyn/XtKT+hQVWOr9+URinda/vQ8PRvxJFCk0OBuXYlq78 lcdPNhNHYEU50Y8dW4//Fwo+9r0XCX+JMSqTlAazNr6lpoAr8fgPoQ0c1g8qQxCaECav XIGA== X-Gm-Message-State: AN3rC/6lYHLqJLiidWS+GJ6l/B5Z2kC30LeHbmH9gfaQ6y7Fh8o3Ycom oAP/NE9iKz7ujNqSyTeFn8eQBcv9YQ== X-Received: by 10.176.2.98 with SMTP id 89mr7026163uas.152.1493722422776; Tue, 02 May 2017 03:53:42 -0700 (PDT) MIME-Version: 1.0 Sender: giovanni.g@gmail.com Received: by 10.103.149.211 with HTTP; Tue, 2 May 2017 03:53:42 -0700 (PDT) In-Reply-To: <0b4f249f-f574-2dcc-4485-1d765bc4cb18@gmail.com> References: <0b4f249f-f574-2dcc-4485-1d765bc4cb18@gmail.com> Date: Tue, 2 May 2017 12:53:42 +0200 X-Google-Sender-Auth: 1xAI5CnhGu2HZsi-9hUPYCFX9p0 Message-ID: To: Rowan Collins Cc: PHP Internals List Content-Type: multipart/alternative; boundary=001a113e22de671b1e054e885a8d Subject: Re: [PHP-DEV] Add is_vectorlike($array) function From: giovanni@giacobbi.net (Giovanni Giacobbi) --001a113e22de671b1e054e885a8d Content-Type: text/plain; charset=UTF-8 On 2 May 2017 at 11:55, Rowan Collins wrote: > On 02/05/2017 10:13, Jesse Schalken wrote: > >> Related to the optimisation made by Sara Golemon here: >> https://github.com/php/php-src/commit/c74bc87c74f48bc55541b3 >> bf2fc67d595f58a3b5 >> >> I often define a function like this, which checks if an array is "vector >> like" i.e. has keys 0,1,2..N: >> >> function is_vectorlike(array $a): bool { >> $i = 0; >> foreach ($a as $k => $v) { >> if ($k !== $i++) { >> return false; >> } >> } >> return true; >> } >> >> > +1, I've been thinking of making a similar suggestion. We can bikeshed the > name (it should certainly start with "array_"), but I think there's a very > good case for having an optimised implementation built in, given the > opportunities for short-cutting based on representation details. > > Wouldn't be better to introduce a new native type "vector"? I know it sounds obnoxious introducing a new basic data type, but I think having a workaround like is_vectorlike() wouldn't completely address the problem. Other than serializations I also felt, many many times, the need to also type-hint the input parameters with vector: function do_something(array $a) { if (!is_vectorlike($a)) throw new Exception("I don't like the input"); // do something with $a } I feel like going back to the dark ages when I had to manually check for is_string(), is_int() on the input parameters. Think how much better it would be to be able to: function do_something_ex(vector $a) { // do something with $a } Also it would be perfectly acceptable to implicitly cast a vector to an array, since a vector is basically a packed array. How does it sound? Complete madness? $a = [ "a", "b", "c" ]; is_vector($a); // true is_array($a); // true unset($a[1]); is_vector($a); // false is_array($a); // true --001a113e22de671b1e054e885a8d--