Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:99849 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24823 invoked from network); 12 Jul 2017 01:50:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 12 Jul 2017 01:50:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=mark@shust.com; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=mark@shust.com; sender-id=unknown Received-SPF: error (pb1.pair.com: domain shust.com from 209.85.218.50 cause and error) X-PHP-List-Original-Sender: mark@shust.com X-Host-Fingerprint: 209.85.218.50 mail-oi0-f50.google.com Received: from [209.85.218.50] ([209.85.218.50:33465] helo=mail-oi0-f50.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 4F/82-01782-27085695 for ; Tue, 11 Jul 2017 21:50:43 -0400 Received: by mail-oi0-f50.google.com with SMTP id p188so8215822oia.0 for ; Tue, 11 Jul 2017 18:50:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shust-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=0eKVoBfm5JbgGq8Xr5+WSwrdU2QbbcwZs/2bpAtkMWg=; b=r85ehHuhjSBVLTdgrqqFnU1hGbZaSTYzNXpxL7G7Q+svZcxlQwiV37TK0eImGg/5vu uISgxvFEGl29ZHyGcAOeDm3qpW02BDe5ZOnaupybbwOIwVOHy1u29mWWRo2e+vYjVzXT eCZ2jFmWEgc+LEyiG654HzQgJFwyWxFGP1McHbNDc4n0nfQgVZ7Ob0XtA2c7hZ5PzUlG UNs0ad1+qHDaS7KOnoLTq+sE1S1MbtWwOJRNC2zenv0Q0u7ZH5xGB50dQVYC/QSut9sT aSjmztGpQPFVa+LHEzPWZnDflbMk6VBS5GgCfNExyMHS2LG3Ntm74tGmyAg2c/ghtOhF s5kg== 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=0eKVoBfm5JbgGq8Xr5+WSwrdU2QbbcwZs/2bpAtkMWg=; b=FxUXwKSLA3Afa0UXKDQoAFVMlUahm+64UaXR7BRXz6IZtbm4rzm92kw42aW8h54kI7 J7lsi1uFCkCspjCu382+/Qb9PT854cCANngJ4yvakUFvJ7Q6Zp4Lpn3hxQJJB6eeCb05 g0yJKNohMGknSe74zt012oTusZ9oT5ARPVEcLWIiftge3zx15LQX3isNeL5qMX3Wbs8B ZAe13/vpIbTXfs0sUx3WPcgk2+lB50YwJRpN47s7Nolg1Uuh56OTcPNuZPPzIRXlOlNl 0dJaeyuvaKgNOjs6siPqXP3HMYyPJeIxa9ZMFoy8VcoFZMnG+JVbKBibcUJiYZVF53V9 vdoQ== X-Gm-Message-State: AIVw111dx6pc2IrX0TjIz+Ii+gdLAEtVaX+Pu7KrKWmlPImmAoU5lEpK 0gz6kZM8Rdr7lBs0XPn3G6oi/8/ivUCg X-Received: by 10.202.192.193 with SMTP id q184mr2027440oif.179.1499824240229; Tue, 11 Jul 2017 18:50:40 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Wed, 12 Jul 2017 01:50:29 +0000 Message-ID: To: Aidan Woods Cc: Rowan Collins , PHP internals Content-Type: multipart/alternative; boundary="001a113dd09a1093250554150bfc" Subject: Re: [PHP-DEV] array coalesce operator concept From: mark@shust.com (Mark Shust) --001a113dd09a1093250554150bfc Content-Type: text/plain; charset="UTF-8" Aidan, Fantastic suggestion (@as) -- that is really the succinctness I was initially looking for, and I think the intention makes a lot of sense. My only concern/issue would be to make sure that isn't considered a 'suppressor' -- but it's actual intent is to skip the execution of the foreach to prevent the error/loop from occurring (rather than just suppressing an error). Cheers, Mark On Tue, Jul 11, 2017 at 4:05 PM Aidan Woods wrote: > If you were willing to accept > > ``` > foreach ($foo as $bar) if (is_array) { > ... > } > ``` > > as a solution, then you might as well use > > ``` > if (is_array($foo)) foreach ($foo as $bar) { > ... > } > ``` > > I wonder if this could be better achieved by expanding what the error > suppression operator `@` can do? This entire behaviour seems more like an > error suppression action on `foreach` to me, otherwise should we consider > coalescing operators for other types/creating a more generic one? > > Going back to the error suppression operator: > > e.g. perhaps > > ``` > foreach ($foo @as $bar) { > ... > } > ``` > > could prevent skip past execution of the entire foreach block if there is > an error using $foo as an array. So might make most sense to place the `@` > on `as`, IMO, but I guess arguments could be made to place it like > `@foreach ($foo as $bar)` or `foreach @($foo as $bar)`. > > > Regards, > Aidan > > On 11 July 2017 at 20:06, Mark Shust wrote: > >> Thanks for the great feedback. >> >> Based on the last mindset on keyword syntax, this comes to mind, intended >> to be used similarly to the 'use' keyword when used within the context of >> a >> closure: >> >> foreach ($foo as $bar) if (is_array) { >> ... >> } >> >> >> I don't think this is a vast improvement over wrapping this within an >> is_array check, however it does avoid the additional nest/wrapping. I was >> hoping for something that reads a bit more concisely or with a bit more >> syntactical sugar than the above. I think this does read nicely though. >> >> Cheers, >> Mark >> >> On Tue, Jul 11, 2017 at 1:50 PM Rowan Collins >> wrote: >> >> > On 11 July 2017 16:02:18 BST, Mark Shust wrote: >> > >For a syntactic >> > >sugar/improvement, this can be shorthand for executing the loop instead >> > >of >> > >wrapping the block within an is_array check: >> > > >> > > >> > >> > > >> > >$foo = "abc"; >> > > >> > >foreach (??$foo as $bar) { >> > > >> > > echo $bar; >> > > >> > >} >> > >> > Hi! >> > >> > I think there's definitely the start of a good idea here, but the syntax >> > you suggest doesn't read quite right. As has been pointed out, this >> differs >> > from existing features in two ways: >> > >> > - the special handling is for any non-iterable value, not just null or >> > empty/falsey values, for which you could use $foo??[] and $foo?:[] >> > respectively >> > - the handling is to skip the loop, not loop once assigning $bar to the >> > scalar value, as would happen with (array)$foo >> > >> > The challenge, then, is to come up with some syntax that somehow >> suggests >> > these rules. The "??" is too much like the null coalesce, which would be >> > misleading. >> > >> > The only idea that immediately comes to mind is a keyword: >> > >> > foreach ifarray ($foo as $bar) { >> > >> > I can't say I'm that keen on that syntax, but maybe it will inspire >> > someone else. >> > >> > Regards, >> > >> > -- >> > Rowan Collins >> > [IMSoP] >> > >> > > --001a113dd09a1093250554150bfc--