Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102330 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 90995 invoked from network); 20 Jun 2018 16:20:10 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jun 2018 16:20:10 -0000 Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=levim@php.net; sender-id=unknown Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.54 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.215.54 mail-lf0-f54.google.com Received: from [209.85.215.54] ([209.85.215.54:39272] helo=mail-lf0-f54.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 60/37-32156-8BE7A2B5 for ; Wed, 20 Jun 2018 12:20:09 -0400 Received: by mail-lf0-f54.google.com with SMTP id t2-v6so189084lfd.6 for ; Wed, 20 Jun 2018 09:20:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=czmWHSZPQWZQzDqcYQfJs7jA4LRa6xF+ORo/Hc/q6z0=; b=lugw1b5G3xUDIdm5sKGgEGa11RdCsY4k1M4mcONjBopOids+scCw7iRBoo6vjmlNpT megoZutuz6p7P4eXaH66xjSGJHzXGrPWQE6tnUmkoyiMxD4S9ZMlCLRK+MjSGNP2mM5c qhYdY7auTV53LVK+rMXdXFT0N6cnCsxDk7s/X5v8FGvtRQuCnJH2cZGzgsP3wnv/tBiH j+QCBl+uXI3JFd5yzslFABc4Hzbo8kiHcmSCKSfgLJGW0ttpmvhMedM027d8i2UKKcgL YaEdF8JW/yqj6S5zUl6xsQ8WIApAq8Ef7lo9JBO8wY69YcttrKG21/0viNQ1wTVd/TgS PVNQ== X-Gm-Message-State: APt69E3fW/TGDwd4rASHcMNM44FQ/um8h5X3x2l+EMS5QV4YCdFkXY06 by+PsrNB0qe5U+M95JPuWKbqTPVbQ//4ZiG390w= X-Google-Smtp-Source: ADUXVKKSeDbPGUdfYKVHSJk2g+s2JldC6XGkCJK+vzBKJWkMS5QfLRpzjAIyCVqHLOMf1SwTFlYh016+v4dEfwU2abs= X-Received: by 2002:a2e:200f:: with SMTP id g15-v6mr14613328ljg.63.1529511605409; Wed, 20 Jun 2018 09:20:05 -0700 (PDT) MIME-Version: 1.0 References: <75db877c-ef0b-7d3b-48a8-9879d90352b0@web.de> <76e4744f-6722-ee49-0325-e4433236f1b1@web.de> <73ac8914-8c9f-e74b-a4f5-f6e39cd96f8f@gmx.de> <3242366.BL9MUqXe46@vulcan> <154ba124-2915-ca36-fa39-5df6726d3b5a@web.de> In-Reply-To: Date: Wed, 20 Jun 2018 10:19:53 -0600 Message-ID: To: enno.woortmann@web.de Cc: internals Content-Type: text/plain; charset="UTF-8" Subject: Re: [PHP-DEV] [RFC][Under Discussion] Add functions array_key_first()and array_key_last() From: levim@php.net (Levi Morrison) On Wed, Jun 20, 2018 at 10:03 AM Woortmann, Enno wrote: > > Am 20.06.2018 um 15:55 schrieb Levi Morrison: > > Your wish cannot be granted for `array_value_last` and > > `array_value_first`; you cannot know by itself if there is a failure > > condition. > > I can comprehend your issue with the lack of distinctness between the > error case and the 'real' array value null without further checks. > But in my opinion the required additional checking of the $input is a > learned behaviour from existing functions which handle single value > elements from an array as reset(), current() and end() will return false > for empty arrays or null for invalid data types which are both also > valid element values. > array_pop() and array_shift() also behave in a comparable way as they > return null as well for empty arrays as for non array input. > > This is not "clean". In contrast here is with my proposal: > > > > if ([$key, $value] = array_last($input) { > > // do something > > } > I still think that this proposed syntax is unintuitive and hard to > comprehend for the user of the function. > I don't think many PHP developers out there know that the condition > inside the if will evaluate to null if array_last returns null. > > Additionally I don't know about any other function where a construct > like this has to be used to extract two or more different semantic > entities, in this case the key and the value, out of a PHP core function > return value. Bad behavior of existing functions does not mean we should create new functions with the same bad behavior. If you believe lack of education regarding array destructuring is an issue then we can use this example specifically in the docs for these functions; problem solved. We can do better and I've shown how we can do better. Please adopt it.