Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:104757 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 78914 invoked from network); 15 Mar 2019 20:49:28 -0000 Received: from unknown (HELO mail-lj1-f196.google.com) (209.85.208.196) by pb1.pair.com with SMTP; 15 Mar 2019 20:49:28 -0000 Received: by mail-lj1-f196.google.com with SMTP id z20so8662191ljj.10 for ; Fri, 15 Mar 2019 10:40:09 -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:content-transfer-encoding; bh=/KOrKVIssvTdjZqiG9r6xbf4UlFVm6rbIZp+Dly7GXY=; b=Lq2E6kgfpfwjD6hOqKWaU9TR8fSMJieabeFHAv3meXXYBaBc0tvxD2BfJMnz1ksB0i D9gGIumMHvD+VDD38zTF/9/S36BA92blKEvdacMr30974a13j4tQ4OiUmhzFD1993+sS qpWQit18tQyf9z3mNpWqaPPWxCqZOtnx4z2HC7q2ZR00+2Puk4cIb3my9Qz3f25r1xk0 9wMcZzoez9O+ql2isTBtBZSiZif5yVAJcByjAlQO09iZ5wKuwG8QqUjISZmTtvYJwLvv CwKTxufMxFRvxbGK86lcvMA4ofavcaVlpYhK/N1AgP5akv7cTexwyTTHn5OsyfGOG62z H37g== X-Gm-Message-State: APjAAAUkCVXdRRLHtgoEFlajk1AEcq+/YZP+LaUU21i29K5GDHXAgRw6 IedXDJ3acxkbQ29aj66manFZzbgsBy4VJ/+pt0I= X-Google-Smtp-Source: APXvYqzaQE0uNld40m/MGbv3OL82TGyiTAXY6To5Qo70m/DCzo01GReJp7gS9QXhim+sf7dSXVppSW0AgYSrvr2aqgI= X-Received: by 2002:a2e:3211:: with SMTP id y17mr2807167ljy.88.1552671608724; Fri, 15 Mar 2019 10:40:08 -0700 (PDT) MIME-Version: 1.0 References: <5c8b20af.1c69fb81.c8b33.d2faSMTPIN_ADDED_MISSING@mx.google.com> In-Reply-To: Date: Fri, 15 Mar 2019 11:39:51 -0600 Message-ID: To: Nikita Popov Cc: Kenneth Ellis McCall , internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [Proposal] Struct Data Types From: levim@php.net (Levi Morrison) On Fri, Mar 15, 2019 at 11:11 AM Nikita Popov wrote: > > On Fri, Mar 15, 2019 at 4:26 PM Nikita Popov wrote= : >> >> On Fri, Mar 15, 2019 at 4:16 PM Levi Morrison wrote: >>> >>> Personally, I think pass by-value with copy-on-write semantics like >>> arrays is the sweet spot. Mutability is fine if it is localized. >> >> >> If we introduce something like this, I think it is very important that i= t does not use the same property access syntax as ordinary objects, which a= re not copy-on-write. I do not want to be second guessing whether $x->y =3D= $z is going to copy or not. Possibly such struct types should indeed use t= he array access syntax, because array access is generally copy-on-write (th= e exception being ArrayAccess objects). >> >>> >>> I think having a no-constructor construction syntax is also a good >>> idea. I've discussed it with a few people, but don't can't remember if >>> it's ever been brought up on-list. Basically, the idea is to use a >>> JSON-like syntax with the type-name in front, and I think it can be >>> fine to use on regular classes that don't have a constructor too: >>> >>> struct Point2d { >>> float $x; >>> float $y; >>> } >>> >>> $origin =3D Point2D { x: 0, y: 0 }; >>> >>> This syntax has no conflicts. We could also add an object literal >>> syntax for creating ad-hoc stdClass objects like so: `$obj =3D { x: 1, >>> y: 1}`. This has an ambiguity, but I believe I have solved the issue >>> without too much trouble once before. >> >> >> This syntax has no conflicts in the sense that the grammar is unambiguou= s, but I believe it does conflict under LR(1). The problem is the alternati= ve array access syntax, which makes Point2D { x } currently valid syntax. W= e can distinguish it at the ":", but by this point a shift/reduce descision= on "{" must have already been made. (We can likely hack around this.) > > > Taking back this one: Turns out that we currently don't support the CONST= ANT{$x} syntax -- it's an oversight from the uniform variable syntax RFC, w= hich generally established syntax parity for the [] and {} array access syn= tax, but missed support for this particular case. In this case it happens t= o be in our favor :) > > Nikita I have a proof-of-concept grammar for the brace construction syntax (without anonymous object literals) here: https://github.com/morrisonlevi/php-src/tree/brace-construction Right now it discards the type name, and then compiles the brace-construction as an array. At least in the short term, I won't be fleshing it out at all, so if anyone wants to take it and run, that's fine. I need to be fixing up the co- and contra-variance patch for 7.4 and doing QA on it.