Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:75159 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 5941 invoked from network); 1 Jul 2014 07:12:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 1 Jul 2014 07:12:37 -0000 Authentication-Results: pb1.pair.com smtp.mail=nikita.ppv@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=nikita.ppv@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.179 as permitted sender) X-PHP-List-Original-Sender: nikita.ppv@gmail.com X-Host-Fingerprint: 209.85.214.179 mail-ob0-f179.google.com Received: from [209.85.214.179] ([209.85.214.179:57823] helo=mail-ob0-f179.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 03/10-03359-46F52B35 for ; Tue, 01 Jul 2014 03:12:36 -0400 Received: by mail-ob0-f179.google.com with SMTP id uz6so10063327obc.24 for ; Tue, 01 Jul 2014 00:12:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=aTwMvySMNMVgNIem9SO286Px+jo6AjgkHzlhQNNtS1E=; b=tcYWIdv60rAC4Ua42aT71fXab2EpPV8jVdZAtAtawtNAtSPxp33gvQ9QS+PKPhlg73 98WtSr7+bkRPtyng1aenkIxAdn0JaihTqbwKTKKEZAbMor++KanKo8QqxIG4laWlsoF6 AUFJXsoYUu/w19Cl5s32ApGeZ7HowvVj5aY9S/lvGRgXC5re1yzkpDl6vKkS9XAYJlnA Ma1G6SVFPzHwLhVMfJ84aaZGPOGv9elC5VlMYhNzNOwsgkv7Pr9eCRZBYyhcedqaaZWx vDrVpuJTSf6PMq7FTaa/qu5FAckrI1TSqK0/OszM52yj1PuggUaBqIeIyswuLliJwt+a O9CQ== MIME-Version: 1.0 X-Received: by 10.182.87.73 with SMTP id v9mr47314184obz.25.1404198753087; Tue, 01 Jul 2014 00:12:33 -0700 (PDT) Received: by 10.182.59.45 with HTTP; Tue, 1 Jul 2014 00:12:33 -0700 (PDT) In-Reply-To: References: Date: Tue, 1 Jul 2014 09:12:33 +0200 Message-ID: To: Dmitry Stogov Cc: Andrea Faulds , Bob Weinand , Xinchen Hui , David Soria Parra , Julien Pauli , PHP Internals Content-Type: multipart/alternative; boundary=089e013cb6e4df374f04fd1c7fc4 Subject: Re: [PHP-DEV] Wired constant expression syntax and bug From: nikita.ppv@gmail.com (Nikita Popov) --089e013cb6e4df374f04fd1c7fc4 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable On Tue, Jul 1, 2014 at 7:48 AM, Dmitry Stogov wrote: > On Tue, Jul 1, 2014 at 1:05 AM, Andrea Faulds wrote: > > > > > On 30 Jun 2014, at 20:06, Dmitry Stogov wrote: > > > > > Hi Bob, > > > > > > I'm wondered why you introduced this wired syntax in PHP-5.6. > > > > Do you mean weird? > > > > yes :) sorry > > > > > > > > > > class FooBar { > > > const bar =3D ["bar" =3D> 3]["bar"]; > > > } > > > > > > It wasn't a part of RFC, it wasn't covered by tests, and it actually > > > doesn't make a lot of sense. May be it's better to remove it? > > > > I disagree, it makes perfect sense: > > > > class FooBar { > > const FOO =3D 3; > > const BAR =3D [ > > 3 =3D> =E2=80=98qux=E2=80=99, > > 4 =3D> =E2=80=98bang=E2=80=99, > > 7 =3D> =E2=80=98theta=E2=80=99, > > 9 =3D> =E2=80=98epsilon=E2=80=99 > > ][FOO]; > > } > > > > ?: and ? only work when there are just two possibilities. > > > > ?: may work with brackets, but I have to admit that it's less readable. > > class Foo { > const X =3D 2; > const Y =3D (Foo::X =3D=3D 0 ? 1 : > (Foo::X =3D=3D 1 ? 2 : > (Foo::X =3D=3D 2 ? 3 : > (Foo::X =3D=3D 3 ? 4 : > 5)))); > } > > The situation is really inconsistent because we allow expressions on > constant arrays but at the same time prohibit array usage. > As we support the [..., ...][...] syntax in normal PHP code as well, I think it's reasonable to allow it here as well. Of course it isn't very practically useful, but there doesn't seem much point to explicitly disallowing it here. > Look into the following scripts: > > > class Foo { > const BAR =3D [ > 3 =3D> =E2=80=98qux=E2=80=99, > 4 =3D> =E2=80=98bang=E2=80=99, > 7 =3D> =E2=80=98theta=E2=80=99, > 9 =3D> =E2=80=98epsilon=E2=80=99 > ][1]; > } > var_dump(Foo::BAR); // works - prints 'bang' > ?> > > class Foo { > const BAR =3D [ > 3 =3D> =E2=80=98qux=E2=80=99, > 4 =3D> =E2=80=98bang=E2=80=99, > 7 =3D> =E2=80=98theta=E2=80=99, > 9 =3D> =E2=80=98epsilon=E2=80=99 > ]; > } > var_dump(Foo::BAR); // doesn't work - Fatal error: Arrays are not allowed > in constants at run-time > ?> > > class Foo { > const BAR =3D [ > 3 =3D> =E2=80=98qux=E2=80=99, > 4 =3D> =E2=80=98bang=E2=80=99, > 7 =3D> =E2=80=98theta=E2=80=99, > 9 =3D> =E2=80=98epsilon=E2=80=99 > ]; > } > // This works again! We may declare array class constants if we don't use > them? > ?> > > Where is the logic? > The reason here is probably that we cannot detect whether something will be an array or not until runtime constant updating. E.g. const BAR =3D FOO ? 1= : [1] or similar. However we might want to detect the case where an array is the root of the AST, as that is invalid for sure and the most common case. Another small issue with the constant expressions I noticed is that our recursion detection doesn't properly work for constant ASTs. For example: