Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:107856 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 39638 invoked from network); 25 Nov 2019 19:39:09 -0000 Received: from unknown (HELO php-smtp3.php.net) (208.43.231.12) by pb1.pair.com with SMTP; 25 Nov 2019 19:39:09 -0000 Received: from php-smtp3.php.net (localhost [127.0.0.1]) by php-smtp3.php.net (Postfix) with ESMTP id C5C212D201B for ; Mon, 25 Nov 2019 09:33:35 -0800 (PST) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp3.php.net X-Spam-Level: X-Spam-Status: No, score=-0.2 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,HTML_MESSAGE,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,URIBL_SBL, URIBL_SBL_A autolearn=no autolearn_force=no version=3.4.2 X-Spam-ASN: AS3215 2.6.0.0/16 X-Spam-Virus: Error (Cannot connect to unix socket '/var/run/clamav/clamd.ctl': connect: Connection refused) Received: from mail-qt1-x82f.google.com (mail-qt1-x82f.google.com [IPv6:2607:f8b0:4864:20::82f]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by php-smtp3.php.net (Postfix) with ESMTPS for ; Mon, 25 Nov 2019 09:33:35 -0800 (PST) Received: by mail-qt1-x82f.google.com with SMTP id g24so11245697qtq.11 for ; Mon, 25 Nov 2019 09:33:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=DFAWFM2FYED6hwH7brqknsdmdGNFORoJlJUHzXL4c3k=; b=aNbd5Uh2UmXHvCOZ6ihbPvmno9fy51zoR+jLG3VcKdszw1qQFLqMcLu2YgcuQkXZ0u wWWUkr63GDbJVnWVW8tkLkuU/B6b1nSONLHH8uGOKyncUDPGcKiatYT/Oet55s8LyJgJ zFqWsM6wYiyKBE3CQRFdgd0Av//dag/Q0pflCbNV/wGCUpGhUWP4x2HdBcx9UXi2ytpH 5EGYEMQYsGT7ITduPya/7dU1bW77K/+fZcIFFtBx3UcXHGSAGwIArhasAkuz+nmaTJTn ePJNcyMNaSajmVfpcGWRMnaxsuYXhd40Nvq/x+gHxzjV2g4Z45fxuyBsasBkTbjuopkl dq5g== 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=DFAWFM2FYED6hwH7brqknsdmdGNFORoJlJUHzXL4c3k=; b=Su8Oojq92j32gFHsb3TUpI69nH+weEWk0xT3nAZA8zkR2wIc9WqDRMl9BzME+7wHcA q5yMIPd7u9P8XutmxWQGddTt6+ZRJh3FK3Gf1ijRaVSPWwPhRceJrp6b7dVqdoIwp2oY 2xLhG6N/y61nWTsw0ExxFwapA7UY/3UIhTKHL777KAUK5fc7f6vDs8+nk1Py42sk2VRD JHENPiD9g/MFokKHmhfUDl3DyrisOKmVeaCp+k1GwdciTHEzG0OpTKxLuDmcktB5Mkc4 R33tddm7XhbnB/axH/0IXWeKKip8n6O7XsiSskfOSnbyu9gC1HSHunfZM/4+jKUvjxub Bhpw== X-Gm-Message-State: APjAAAWJqjcl1gWrZ2nFJa1+p2s+D4JeoY71EVTOzp3g6Fyu4kUimolX w9BnSQWjy9ActOfXMw/8cPDEdlsZJmI= X-Google-Smtp-Source: APXvYqz650kM/G7qGK9eo5g0dOSGen3p9ovYtm31Aj9oQt0qb+uf8x/Wd6aSeoVZmd6bpj7/cQJoXA== X-Received: by 2002:ac8:690c:: with SMTP id e12mr17736165qtr.239.1574703213900; Mon, 25 Nov 2019 09:33:33 -0800 (PST) Received: from mail-qk1-f171.google.com (mail-qk1-f171.google.com. [209.85.222.171]) by smtp.googlemail.com with ESMTPSA id x1sm3679859qke.125.2019.11.25.09.33.32 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 25 Nov 2019 09:33:32 -0800 (PST) Received: by mail-qk1-f171.google.com with SMTP id d202so5169666qkb.1 for ; Mon, 25 Nov 2019 09:33:32 -0800 (PST) X-Received: by 2002:a05:620a:14bc:: with SMTP id x28mr9823828qkj.494.1574703211841; Mon, 25 Nov 2019 09:33:31 -0800 (PST) MIME-Version: 1.0 References: <861afae0-4568-745f-6615-a252067cc506@aimeos.com> <003801d5a2df$801404e0$803c0ea0$@jhdxr.com> In-Reply-To: <003801d5a2df$801404e0$803c0ea0$@jhdxr.com> Date: Mon, 25 Nov 2019 18:33:20 +0100 X-Gmail-Original-Message-ID: Message-ID: To: CHU Zhaowei Cc: "Aimeos | Norbert Sendetzky" , PHP internals Content-Type: multipart/alternative; boundary="000000000000918bca05982f29fe" X-Envelope-From: Subject: Re: [PHP-DEV] [RFC] "arrayable" pseudo type hint From: andreas@dqxtech.net (Andreas Hennings) --000000000000918bca05982f29fe Content-Type: text/plain; charset="UTF-8" On Sun, 24 Nov 2019 at 16:55, CHU Zhaowei wrote: > > This proposal is not obsolete by the implementation of union types (i.e. > > array|Traversable) because union data types are types/interfaces > > combined by OR. "arrayable" is a combination of OR and AND: > > > > array|ArrayAccess&Countable&Traversable > > True. But it's also easy to implement in userland. Just create a new > interface that extends these three interfaces, then union types can work. > > ```php > interface Arrayable extends ArrayAccess, Countable, Traversable {} > > function array_x (array|Arrayable $arr) {} > ``` > > This is not something that userland cannot implement or very difficult to > implement. IMO it will make php more complex if we include this in the > core. Actually what you suggested has been totally covered by the future > scopes of the union types RFC. or "intersection types": https://wiki.php.net/rfc/intersection_types > If you really want to explore this idea, I think they are worth more > attention, instead of this specific case. > > Regards, > CHU Zhaowei > > > -----Original Message----- > > From: Aimeos | Norbert Sendetzky > > Sent: Wednesday, November 20, 2019 6:21 AM > > To: internals@lists.php.net > > Subject: [PHP-DEV] [RFC] "arrayable" pseudo type hint > > > > Since PHP 7.1, there's the "iterable" pseudo type hint that matches > "array" or > > "Traversable". > > > > PHP frameworks would profit from support of an "arrayable" pseudo type > hint > > that matches "array" and all objects that can be used like arrays. > > > > For that, "arrayable" objects have to implement these interfaces: > > - ArrayAccess > > - Countable > > - Traversable (i.e. either Iterator or IteratorAggregate) > > > > > > Implementing "arrayable" pseudo type, we could pass arrays or all > objects that > > can be used like arrays to methods and do: > > > > function useArrayable( arrayable $arg ) : arrayable { > > $cnt = count( $arg ); > > $value = $arg['key']; > > foreach( $arg as $key => $entry ); > > return $arg; > > } > > > > > > Best use cases are: > > > > - Laravel Collection > > ( > https://github.com/laravel/framework/blob/6.x/src/Illuminate/Support/Collect > > ion.php) > > > > - Aimeos Map (https://github.com/aimeos/map) > > > > > > No new interface is proposed because we can check if objects implement: > > > > ArrayAccess && Countable && Traversable > > > > > > Because "array" !== "arrayable", "arrayable objects will not be accepted > by > > array_* functions and all functions that only use "array" as type hint > for > > parameters and return types. > > > > > > This proposal is not obsolete by the implementation of union types (i.e. > > array|Traversable) because union data types are types/interfaces > > combined by OR. "arrayable" is a combination of OR and AND: > > > > array|ArrayAccess&Countable&Traversable > > > > > > Also, "arrayable" won't supersede "iterable" because > > > > - "iterable" matches arrays, iterators and generators > > > > - "arrayable" matches arrays and objects that can be used like arrays > > > > > > "Can be used like arrays" doesn't include support for empty() because it > works > > for empty arrays but not for "arrayable" objects without elements. > > If possible and requested, this may be addressed by another RFC. > > > > > > As Larry Garfield suggested, this pre-RFC proposes concentrates on the > > "arrayable" pseudo type only. > > > > It does NOT discusses that: > > > > - arrayable objects can be converted to arrays (Steven Wade works on an > RFC > > for a __toArray() magic function) > > > > - ArrayObject or any other arrayable object makes the array_* functions > less > > relevant in the future > > > > - arrayable objects can be passed to array_* functions in the future > > > > -- > > PHP Internals - PHP Runtime Development Mailing List To unsubscribe, > visit: > > http://www.php.net/unsub.php > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > > --000000000000918bca05982f29fe--