Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125058 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 5CE281A00BD for ; Tue, 20 Aug 2024 00:44:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1724114786; bh=jtXz9mH1yaIqdW+zVauX1LWgcK/Oa20za4KyVW5EGqo=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=TFreFrv/ocpJImZDLpX6q35Eot5J0+SYiyp2RLPgsIptj9rasSZNbuCrscJhXTAsz AO2rWQJYyKJ2nUCJceMr0ObnHeHDt/N5GEUuFt8SM4rvFE2y4n99YDM0lOM2tTwjmz UvM9RaozTQdaSWsQ5n93DhCkjI5z39b1S6cSZSmm4nIr1moStuFNek70iEKZwmT6Dz Ld1vjzCU2O4bUdtkcW0qz89We87aCDqbQyymqc2u9y4S2Q5//RRYK9KtbY2zZBJS21 NGuF4W/33RHz5wOlgtTz6YTtBxm1CsDx0Xb7cyxGYWsyIGcwu6qzG3t00hKYUcWq9J VNGPz1vq9FQOQ== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 27F00180034 for ; Tue, 20 Aug 2024 00:46:25 +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.8 required=5.0 tests=BAYES_50,DKIM_SIGNED, DKIM_VALID,DMARC_MISSING,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE autolearn=no autolearn_force=no version=4.0.0 X-Spam-Virus: No X-Envelope-From: Received: from mail-yw1-f181.google.com (mail-yw1-f181.google.com [209.85.128.181]) (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, 20 Aug 2024 00:46:24 +0000 (UTC) Received: by mail-yw1-f181.google.com with SMTP id 00721157ae682-6b47ff8a59aso23176427b3.2 for ; Mon, 19 Aug 2024 17:44:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20230601.gappssmtp.com; s=20230601; t=1724114675; x=1724719475; darn=lists.php.net; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=nqbbUuKmAkuUVu7uQ8uzmtjyqEqcDCVBUjgQbi6x/hs=; b=rAc37plASb8x0Hu77UpvYAa/JlfgYwmtk2jFeE1ll2COk3/jQomyIYvt8ZEZY+cuUC hXE7esP+se+VFt1jJVn/s9M4M1LxsR0W+2aYJaS7yO8GaLgS46oJr+V1A7+tr5lXZozV FI/tZ8bLcPRJ7yz6oro/1hIt8EqpIUI7TWEv10Rh8swy2Kc6aXzbJU+s+Toyy5QEH/TC dPfYb+nR0biBlFlCrZiogQDjWrZ5PhLdogUFFLagkDrHZHNKVe2cC0pUoClXK2z0WyHX AQb4M34u47sOPvGEtYX38MoLOsswLjcnNy0ZnwoORvl4x2P2465Nbbe/oZ2t9dqfLXCb 8CuA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1724114675; x=1724719475; h=to:references:message-id:content-transfer-encoding:cc:date :in-reply-to:from:subject:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=nqbbUuKmAkuUVu7uQ8uzmtjyqEqcDCVBUjgQbi6x/hs=; b=oIVq+u4sWNgx5BsY8Mg331RfNCVKDRcGK0zp6lx8Arj5UW+ozgGqo9xJnTccN4ajqQ 4ODAW+n8pesUXTHMO7JxrkUGJ8+0wOK7o2X00WgdXDXo6LWXza8LDAW6zKR01MWgqIBZ npFR3f/jugfyYwCPY2t6Rummsq+Lbl06sCiOfddhdd73vFuRHEkoOKVBOhZyIdkIRcD0 qnG0pshiDWNlYmc2qAlQrAJ5O6hqGVIKOWKy0ovIS5/ark2XrVN9R/WcQOz46zD+W30c txKLdv7SUKSNYIfeSSzT+a0HKUwxnhUQo7sDrf1IGHGPAloSlETvIiNix0Zv6K5rq+OH fwMg== X-Gm-Message-State: AOJu0YxCjT1wvJ1d2utIBhCjQKD5Gi0D23FrKXk6l87P4kc/ElZIT7hP mgDRvuVOMSNHv/4Wri9lqluf+I+C3H3Ne6hgr4hlYYNB83dY3q24XRk8HW9kXG5yGN588hJ2zF7 Ck0U= X-Google-Smtp-Source: AGHT+IFnFvqdVJSsA4Q9Cpgadzy3GvKPqb3DvFLGVgFDcLUvy3zJvlHBvuYSMmqfnQTFnJb6JvhAUg== X-Received: by 2002:a05:690c:2f0b:b0:6ad:ba92:1731 with SMTP id 00721157ae682-6b1be7c294emr110892087b3.41.1724114674588; Mon, 19 Aug 2024 17:44:34 -0700 (PDT) Received: from smtpclient.apple (c-98-252-216-111.hsd1.ga.comcast.net. [98.252.216.111]) by smtp.gmail.com with ESMTPSA id 00721157ae682-6af9e1d9e31sm17389087b3.143.2024.08.19.17.44.33 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 19 Aug 2024 17:44:33 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Precedence: bulk list-help: list-post: List-Id: internals.lists.php.net x-ms-reactions: disallow Mime-Version: 1.0 (Mac OS X Mail 16.0 \(3696.120.41.1.8\)) Subject: Re: [PHP-DEV] State of Generics and Collections In-Reply-To: <1b59392a-68cb-36eb-0fef-977ac7113520@php.net> Date: Mon, 19 Aug 2024 20:44:32 -0400 Cc: PHP Developers Mailing List Content-Transfer-Encoding: quoted-printable Message-ID: References: <1b59392a-68cb-36eb-0fef-977ac7113520@php.net> To: Derick Rethans X-Mailer: Apple Mail (2.3696.120.41.1.8) From: mike@newclarity.net (Mike Schinkel) > On Aug 19, 2024, at 1:08 PM, Derick Rethans wrote: >=20 > Hi! >=20 > Arnaud, Larry, and I have been working on an article describing the=20 > state of generics and collections, and related "experiments". >=20 > You can find this article on the PHP Foundation's Blog: > = https://thephp.foundation/blog/2024/08/19/state-of-generics-and-collection= s/ >=20 > cheers, > Derick Great job on providing so much detail in your blog post. JMTCW, but I am less of a fan of boil-the-ocean generics and more of a = fan of focused pragmatic solutions like you proposed with the Collection = types. The former can result in really complex to read and understand = code whereas the latter =E2=80=94 when done well =E2=80=94 results in = easier to read and understand code. =20 It seems Java-style Generics are viewed as the proper archetype for = Generics in PHP? I would challenge the wisdom of taking that road = considering how different the compilers and runtimes are between the = Java and PHP. PHP should seek out solutions that are a perfect fit for = its nature and not pursue parity with Java. =20 As PHP is primarily a web development language =E2=80=94 vs. a systems = language like C or Rust, or an enterprise application language like Java = or C# =E2=80=94 reducing code complexity for reading and understanding = is a very important attribute of the language. PHP is also a unique language and novel solutions benefit a unique = language. PHP should pursue solutions that result in less complex code = even if not found in other languages. Your collections idea is novel =E2=80= =94 which is great =E2=80=94 but there are probably even more novel = solutions to address other needs vs. going full-on with Java-style = generics.=20 Consider if adding type aliases; or augmenting, enhancing, or even = merging classes, interfaces, and/or traits to address the needs = Java-style generics would otherwise provide. I would work on some = examples but I think you are more likely to adopt the features you come = up with on your own. -------- As for type-erasure, I am on the fence, but I find the proposed "how" = problematic. I can see wanting some code to be type-checked and other = code not, but I think more often developers would want code type-checked = during development and testing but not for staging or production. And if = the switch for that behavior is in every file that means modifying every = file during deployment. IMO that is just a non-starter. If you are going to pursue type-erasure I recommend introducing a file = in the root =E2=80=94 call it `.php.config` or similar =E2=80=94 that = contains a wildcard enabled tree-map of code with attributes settable = for each file, directory, group of files and/or group of directories = where one attribute is type-checked or other attributes are reserved for = future use. This config file should also be able to delegate the = `.php.config` files found elsewhere, such as config files for each = package in the vendor directory. It would be much better and easier to = swap out a few `.php.config` files during CI/CD than to update all = files. Additionally PHP could use an environment variable as prescribed by 12 = Factor apps to identify the root config file. That way a hosting company = could allow someone to configure their production server to point to = `.php.production.config` instead of ``.php.development.config`.=20 -Mike P.S. Also consider offering the ability for a function or class method = to "type" a parameter or variable based on an interface and then allow = values that satisfy that interface structurally[1] but not necessarily = require the class to explicitly implement the interface.=20 This is much like how `Stringable` is just automatically implemented by = any class that has a `__ToString()` method, but making this automatic = implementation available to userland. Then these automatically-declared = interfaces can cover some of the use-cases for generics without the = complexity of generics. For example =E2=80=94 to allow you to visualize =E2=80=94 consider a = `Printable` interface that defines a `print()void` method. If some PHP = library has a class `Foo` and it has a method with signature = `print()void` then we could write a function to use it, maybe like so: --------- interface Printable { print($x any)void } // The prefix `?` on `Printable` means `$printer` just has to match the = `Printable` interface's signature function doSomething($printer ?Printable) {=20 $printer->print() } $foo =3D new Foo(); doSomething($foo); --------- Something to consider? =20 [1] https://en.wikipedia.org/wiki/Structural_type_system