Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:123433 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 E96311A009C for ; Mon, 27 May 2024 04:13:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1716783286; bh=dXJRGBVPUhEMlJtpvyKZBSoLCpS/APsA6E0e7KSOFwE=; h=From:Date:Subject:To:From; b=i6Bk6gBs09CpuIRM5FjIxakglFI//N22p3vtweG/smB97huHONJYnjoi0GKTLuSyJ tpDYA6tnnnzfLl+XRSjnoEyiiivOElTKVoCkz6SzmLTZadzlreNUDTjMqEEYgsTtj3 30PNXlJ5f50vJ8NHFRu8Ca76Wxtp9N/Up5AJUMCbfgsPrAh3myffx5WpGrjcwuY3Sp E5iyevIiaVeqRngMLu6ye7wINodDF6vYwT0C3Wyup5nfRquxulvX2LtpWBZxj2p8MW /8wrh3x25aqhAN2Oxu1dPxqHnx0ykBVA1AVzEVUjGL98aLw5AlQzXXJwBIKie7gne3 HRy23zvMrW8gg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E1113180069 for ; Mon, 27 May 2024 04:14:45 +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, HTML_MESSAGE,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: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) X-Envelope-From: Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (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 ; Mon, 27 May 2024 04:14:45 +0000 (UTC) Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-42108739ed8so29052095e9.0 for ; Sun, 26 May 2024 21:13:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1716783225; x=1717388025; darn=lists.php.net; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=qmUA52j8zo2hVPXtCYEv7dm5ONHeqLBMZQ7BY1ae1Pg=; b=NA7qkuD9X3TcgvPjR4o/zXwYwv99gLE9A9MhzdsgPf8jH0DDiZjKJhAljQW4FBCFHG voBYRPIkPUXqjg7YqUlYJYxOVK63wqDRGhyYUnD4tAVZSyKlQNJUD1ta/fbACbcnUTy9 tewKNs8MMdWsVtRWGbgBSYs0ALzUHqAE5hNdwwOlU+ON2RGI55v3pHBpQ6McjjWQFE0y GVOM9x3z/XHyIXf1CGiN9d7XW+GffK6nqtMVkhNMU3JAM81Qbta1IT5LeleX7JyiIAsI wkDai497m9TZkXJ91wxR6Xz4f/5CrV89As56cBLU+VpdPccxiiyafA4JXKWQw29soUmY FwAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716783225; x=1717388025; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=qmUA52j8zo2hVPXtCYEv7dm5ONHeqLBMZQ7BY1ae1Pg=; b=ee1D1L+C0PjP02axkVd772RxrFWPurXH+McbEjyak9M8OzrkkHIX0hrG+aJRVVgdqQ l+gs9CLwMiQZ0w7eQhIyeCPjLWyzt/OAyQVzgf14sXSKDFh20Xxq2BmbpNhee/TJed3q +t4pYB8eV1lwteAqElQ9yM0wpU0Ajx45I/mdkd5QyBQBUKgHvCg+/6+e+007JLxYBytw D3RO8ZWvS8JVg9dWTmcQzLzF5++tOQYAf/KsZuD8rraslww9+ZsQUGaNeCgbJJxMfVRQ 14LmEE4USIgIunFCnl7I9vXUxt8EsnPiOF73qVQNV3LuttuudzXupgH8vp5ZKxUAuitA ZaKQ== X-Gm-Message-State: AOJu0YyU8bEMxvwC7uPurn36SmYUYAU77+Hw2a4hSKqHXtV5JKdF2ElH sT+rBvOBZXVsm3rlaAKobUNKJa+OCGnm2h3VvS1L77Gazaun1GukpTqYaYwv8JnOy39UmtY/6AV +LEOhngW4sBABLiOtmckU2QcHlmttvZ8= X-Google-Smtp-Source: AGHT+IEmtjgoLa92Rpa/py96cn7i50SpAa0tDNJdCUgB0ZQAJZowG5klk5b+jLfQ6FHugX+wj4jbE4H1Q2PEYEmLcGQ= X-Received: by 2002:a7b:ca48:0:b0:420:182e:eb46 with SMTP id 5b1f17b1804b1-42108aa7bc5mr85729545e9.38.1716783224841; Sun, 26 May 2024 21:13:44 -0700 (PDT) Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net MIME-Version: 1.0 Date: Mon, 27 May 2024 07:13:33 +0300 Message-ID: Subject: [PHP-DEV] Extract ArrayAccessRead from ArrayAccess To: php internals Content-Type: multipart/alternative; boundary="00000000000045ed0f061967bec5" From: udaltsov.valentin@gmail.com (Valentin Udaltsov) --00000000000045ed0f061967bec5 Content-Type: text/plain; charset="UTF-8" Hello, internals! Recently I was implementing some collections and realized that I cannot use `ArrayAccess` for immutable collections without throwing `BadMethodCallException` in `offsetSet` and `offsetUnset`. I also cannot have an `interface ReadonlyCollection extends ArrayAccess`, because `offsetSet` and `offsetUnset` are mutators and have `TValue` in a contravariant position. Here's the idea: let's extract `ArrayAccess::offsetExists` and `ArrayAccess::offsetGet` methods into a separate `ArrayAccessRead` interface (name can be different) and allow read operations `$exists = isset($object['key'])` and `$value = $object['key']` for objects of classes that implement this interface. `ArrayAccess` will then extend `ArrayAccessRead` and add the remaining 2 methods. Looks like this change should not break backward compatibility. ``` interface ArrayAccessRead { public function offsetExists(mixed $offset): bool; public function offsetGet(mixed $offset): mixed; } interface ArrayAccess extends ArrayAccessRead { public function offsetSet(mixed $offset, mixed $value): void; public function offsetUnset(mixed $offset): void; } // then interfaces of the doctrine/collections package might look like this: namespace Doctrine\Common\Collections; interface ReadableCollection extends Countable, IteratorAggregate, ArrayAccessRead {} interface Collection extends ReadableCollection, ArrayAccess {} ``` -- Regards, Valentin Udaltsov --00000000000045ed0f061967bec5 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable
Hello, internals!

Recently I was implementing some = collections and realized that I cannot use `ArrayAccess` for immutable coll= ections without throwing `BadMethodCallException` in `offsetSet` and `offse= tUnset`. I also cannot have an `interface ReadonlyCollection extends ArrayA= ccess`, because `offsetSet` and `offsetUnset` are mutators and have `TValue= ` in a contravariant position.

Here's the idea: let&= #39;s extract `ArrayAccess::offsetExists` and `ArrayAccess::offsetGet` meth= ods into a separate `ArrayAccessRead` interface (name can be different) and= allow read operations `$exists =3D isset($object['key'])` and `$va= lue =3D $object['key']` for objects of classes that implement this = interface. `ArrayAccess` will then extend `ArrayAccessRead` and add the rem= aining 2 methods. Looks like this change should not break backward compatib= ility.

```
interface ArrayAccessRead
{
=C2=A0= =C2=A0 public function offsetExists(mixed $offset): bool;
=C2=A0 =C2=A0= public function offsetGet(mixed $offset): mixed;
}

interface Arr= ayAccess extends ArrayAccessRead
{
=C2=A0 =C2=A0 public function offs= etSet(mixed $offset, mixed $value): void;
=C2=A0 =C2=A0 public function = offsetUnset(mixed $offset): void;
}

// then int= erfaces of the=C2=A0doctrine/collections package might look like this:

namespace Doctrine\Common\Collections;
<= br>
interface ReadableCollection extends Countable, IteratorAggre= gate, ArrayAccessRead {}
interface Collection extends ReadableCol= lection, ArrayAccess {}
```
--
Regards, Valentin Udaltsov
--00000000000045ed0f061967bec5--