Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:122900 X-Original-To: internals@lists.php.net Delivered-To: internals@lists.php.net Received: from php-smtp4.php.net (php-smtp4.php.net [45.112.84.5]) by qa.php.net (Postfix) with ESMTPS id 9F38F1A009C for ; Tue, 2 Apr 2024 22:35:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1712097375; bh=uqpyE9f/XI1/bPv9AZfqLMMMzxbCFJ5/xAUF49g0/pQ=; h=References:In-Reply-To:From:Date:Subject:To:From; b=GRz559uYRIW+pIOjv7SbfegIZV0G7EMyvSXU9wGb78+943t1DW5qxee4O3vWUr1TQ 2HgHDqUNKvzUaGDIFIs7+TR1j+tEJ2AtRZUaqKF8hbKAjsOgE/erqgZW1G2aLJoo+d ba9z87xkjJkwpLxN8YPnnMGBGYE7AJoQ4ZXdbbcLA/TZr/ps9EdzI4WZpiDATij0b7 JlzLBfWO+7NZmjKNO4mu5yN2NIJd5OMLShgZS5xJGC0pp/JWH2pp87vy4xAnKGQzSD bqXxGwr1Tr3bixvUlrzZt015t8QI9OxVCyex/CGR1gr12l9sw2TLGrrLR7Iqs9LbyG tbAA6xVUTfPpw== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id DA94E180077 for ; Tue, 2 Apr 2024 22:36:14 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 4.0.0 (2022-12-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=0.6 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,DMARC_PASS,FREEMAIL_FROM, RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-ua1-f54.google.com (mail-ua1-f54.google.com [209.85.222.54]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 2 Apr 2024 22:36:14 +0000 (UTC) Received: by mail-ua1-f54.google.com with SMTP id a1e0cc1a2514c-7e096ab9287so2003339241.2 for ; Tue, 02 Apr 2024 15:35:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1712097345; x=1712702145; darn=lists.php.net; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=LP4FX1YDW6W3JtyizkY3c6V9JKll+sursKkB8MQ5TnM=; b=aIRL2uCpAcZP7sWfZ8PDy52NptxWRIuWInarH0TxStWYjzH4k/uAet9z1W1pRd9RZE 4x2apay7FbNMWdZDHlXDAZ/j3ClOoY53g9AdFMC9l5EqJFySujhVKwSsaglmHIxFwFjv idC7HX8m6cNLTra506TAcaIlWs8Rtvh17I7oAD/I0YH6mKz5j0YXjg9WjaKQTRH1ek5x Q1KufsPvhf4TKadNAHl/fF7SePpcGo34R2U1yFlTiBGIXZtrdYCRzyQ9CNJer5f/Y/3a CQGtSE6G/LM75U/4KYZSigMw5FGWiIX7nZgCV7ApDv3bAE/J+WfkWYv3wJqOhJ3xCn71 gSEw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712097345; x=1712702145; h=content-transfer-encoding:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=LP4FX1YDW6W3JtyizkY3c6V9JKll+sursKkB8MQ5TnM=; b=DDSc0rpbudplWm/z7zDKGcb1YevjYSb05AgQZRhYutVI5YRx9NyBWiM7sZ+uklGOVl tcxzG2EpA9KEX9QLDJwEA3EiV9MRSwTNz/YqmJWonTs16iF1MMII+Yn0HInXOfGiKS2L w5ZMg+hYgBJWja5/iev8Xn+WjSNX88yb9Qep5VTDqhglZxMVu1cutuH5lzQD0QoHQTZU DPFuy/AUs9c6k6V69d1Gqo80ZftdkoqGy8W+QTgYheg+XYMTT4sy/EgJWTTqOMM6Wm2f Nvu0hTpvWfoo0pzXAsiEVKfr3Uea0lQTE8adXOT7n3ZpzXpioJkYX+GyL8lkx3a79EJk R8kQ== X-Gm-Message-State: AOJu0YyVsHpX9ApeS6i25hHanRrPFZ1l8/E14qtlQSTpZetuRdMNEli1 lrM+O7iV3f4p6+jQzouDVZc32fFD3VgYnj32qjAtLv0B0HhqTQtXLjXrrFvo0zQzvUSqlxm8l+2 eCcUB4TFRSQWpqkgovkAgspnX+mV71g3QvnitaQ== X-Google-Smtp-Source: AGHT+IFc6h0yLhFYjYZndhSKH2AC+sk2UAbFzWjCeRr6vldg/7Rwuq+cnwIvjwQrBsm24pcdi5GhsP6I2vZZpOfBrrU= X-Received: by 2002:a05:6102:5c8:b0:478:861c:b3b3 with SMTP id v8-20020a05610205c800b00478861cb3b3mr4860241vsf.9.1712097345156; Tue, 02 Apr 2024 15:35:45 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 References: <9aeebf8e-d6a3-4d55-a926-77433a0bf4e2@gmail.com> In-Reply-To: <9aeebf8e-d6a3-4d55-a926-77433a0bf4e2@gmail.com> Date: Wed, 3 Apr 2024 00:35:34 +0200 Message-ID: Subject: Re: [PHP-DEV] [RFC][Concept] Data classes (a.k.a. structs) To: PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: tovilo.ilija@gmail.com (Ilija Tovilo) Hi Niels On Tue, Apr 2, 2024 at 8:16=E2=80=AFPM Niels Dossche wrote: > > On 02/04/2024 02:17, Ilija Tovilo wrote: > > Hi everyone! > > > > I'd like to introduce an idea I've played around with for a couple of > > weeks: Data classes, sometimes called structs in other languages (e.g. > > Swift and C#). > > As already hinted in the thread, I also think inheritance may be dangerou= s in a first version. > I want to add to that: if you extend a data-class with a non-data-class, = the data-class behaviour gets lost, which is logical in a sense but also su= rprised me in a way. Yes, that's definitely not intended. I haven't implemented any inheritance checks yet. But if inheritance is allowed, then it should be restricted to classes of the same kind (by-ref or by-val). > Also, FWIW, I'm not sure about the name "data" class, perhaps "value" cla= ss or something alike is what people may be more familiar with wrt semantic= s, although dataclass is also a known term. I'm happy with value class, struct, record, data class, what have you. I'll accept whatever the majority prefers. > I do have a question about iterator behaviour. Consider this code: > ``` > data class Test { > public $a =3D 1; > public $b =3D 2; > } > > $test =3D new Test; > foreach ($test as $k =3D> &$v) { > if ($k =3D=3D=3D "b") > $test->a =3D $test; > var_dump($k); > } > ``` > > This will reset the iterator of the object on separation, so we will get = an infinite loop. > Is this intended? > If so, is it because the right hand side is the original object while the= left hand side gets the clone? > Is this consistent with how arrays separate? That's a good question. I have not really thought about iterators yet. Modification of an array iterated by-reference does not restart the iterator. Actually, by-reference capturing of the value also captures the array by-reference, which is not completely intuitive. My initial gut feeling is to handle data classes the same, i.e. capture them by-reference when iterating the value by reference, so that iteration is not restarted. Ilija