Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:102321 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 72554 invoked from network); 20 Jun 2018 13:55:16 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 20 Jun 2018 13:55:16 -0000 Authentication-Results: pb1.pair.com header.from=levim@php.net; sender-id=unknown Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.46 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.215.46 mail-lf0-f46.google.com Received: from [209.85.215.46] ([209.85.215.46:39731] helo=mail-lf0-f46.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 8F/F3-32156-3CC5A2B5 for ; Wed, 20 Jun 2018 09:55:15 -0400 Received: by mail-lf0-f46.google.com with SMTP id t2-v6so5101886lfd.6 for ; Wed, 20 Jun 2018 06:55:15 -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=vZqwLN13F7CnvkeJ8vl0kc1PR0vHB5AY2H2i686xnTw=; b=fqwaPwpBW5LNMiG0fkTy1O1GXWGTjkszGskiTCFQLaHwcgMGRD1SZCp1iyyV/wZ24K 7d3v/CH6ssWLdeSYlGNqSkbB4LwtzatZTYAX8MiKGBakEqtmp2X/RSKZyEyMyVUdtl7q 1dH8CJRtMLjVW8FJ1F3ZPGWjqPN/lf8/AJDgYIGSIEIqNw6HljRrh2Wow/on0jgvAcPK ZEOPd6wNOWZSdFXl4jjpvDD0rT8sDmHFdxivsxSfwnT0vmZAWPC4rh+uGPp8Eln4EyrC 0q5nuA1xj2Y7/3xMfa8OUHBcuhrSfa1F0W/mU8NNp/EQWtPvMrVgR4rby1i/VFwrWYFD olxA== X-Gm-Message-State: APt69E1F5GdU6LM19I1eJfjQuzLHhxZf9oN/tjGjk4VTc37QqK+lediD TfPJ7iGvFB9iumV+LBXNPBOsgxCt39qZnxD7hcE= X-Google-Smtp-Source: ADUXVKIUpxObu4l9ClcIBWsYK1BNW6Dp6+mYgp0UUuVoXQpASTZA3t1AwOQDbVq24mAhENGLxF8T7mL0IXcqs55XikQ= X-Received: by 2002:a19:eb8c:: with SMTP id f12-v6mr7326159lfk.132.1529502912021; Wed, 20 Jun 2018 06:55:12 -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: <154ba124-2915-ca36-fa39-5df6726d3b5a@web.de> Date: Wed, 20 Jun 2018 07:55:00 -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 3:17 AM Woortmann, Enno wrote: > > Hi Levi, > > > Am 20.06.2018 um 04:47 schrieb Levi Morrison: > > list($key, $value) = array_first($input); > > // $key will be null if the call failed > > > > list($key, $value) = array_last($input); > > // $key will be null if the call failed > > Your proposed functions would be implementable with the internal > functions but I think this approach doesn't provide a clean function > interface as it forces the user to evaluate the wanted value from the > returned array structure by using either the list() construct or > something like $key = array_first($input)[0]; > > I believe two functions with this interface will be confusing and less > intuitive for the developer. > If I use a function I expect it to give me a return value which I can > use without any further post processing $wantedValue = > fancyFunction($someInput); Your wish cannot be granted for `array_value_last` and `array_value_first`; you cannot know by itself if there is a failure condition. As proposed you have to write: if (!empty($input)) { $value = array_value_last($input) // do something } This is not "clean". In contrast here is with my proposal: if ([$key, $value] = array_last($input) { // do something } If I need the last/first key and value (which is a real use-case and why I thought of this in the first place) then in your proposal I have to do: if (!empty($input)) { $key = array_key_last($input); $value = array_value_last($input); } Or use the key to get the value; roughly the same amount of code. Either way is not as clean as: if ([$key, $value] = array_last($input) { // do something } Hopefully I have shown that for various use-cases the proposed interface is cleaner and that you cannot argue against it on those grounds. Adding 4 functions which cover 4 use-cases, with ugly names, and 2 of them having poor failure semantics is not a good direction. Adding only 2 functions which cover 6 use-cases, with nice names, and good failure conditions is better. Please reconsider.