Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:121489 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 67372 invoked from network); 27 Oct 2023 20:42:10 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 27 Oct 2023 20:42:10 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 0F0A11804BC for ; Fri, 27 Oct 2023 13:42:09 -0700 (PDT) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on php-smtp4.php.net X-Spam-Level: X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,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=3.4.2 X-Spam-ASN: AS15169 209.85.128.0/17 X-Spam-Virus: No X-Envelope-From: Received: from mail-vk1-f181.google.com (mail-vk1-f181.google.com [209.85.221.181]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Fri, 27 Oct 2023 13:42:08 -0700 (PDT) Received: by mail-vk1-f181.google.com with SMTP id 71dfb90a1353d-49abb53648aso971198e0c.0 for ; Fri, 27 Oct 2023 13:42:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1698439327; x=1699044127; darn=lists.php.net; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=MppUDkRU5BeWFn7yW6jwg0rp58EROm2hoIGaX97f8DQ=; b=FUOePJ00eP+GI7ypu+dZ+LXjL4By5REaPY+H+GVcLuKnlClTvnSa2+Pe4yMKnzeP+n RBsLBqPEIW6wznByc1GugiWrVvttwWvFS45Q5ZOZ0TLsEYYLHRO5vOZ/q+z0EdPDTN2C iBXT4i+JnWQQFAK4jpT4UQYqZK7XDfcSSznr4afuhbmRQgedfrIPawkP0k1ufMGwoPHZ RhyJgzwIHHeVjy8VIlQ3I2FlEC9sl6aWt5SLjwcGrHNlyIhtehheDsBKOrvOp+OO6kce mhIJPSYElTz0fiNvNl7+P3ZI39BG12k5tfv8L4yrzMiDRJ0YwTXAde4PIooOzdU1aX8R TdDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698439327; x=1699044127; h=cc:to:subject:message-id:date:from:references:in-reply-to :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=MppUDkRU5BeWFn7yW6jwg0rp58EROm2hoIGaX97f8DQ=; b=FgCEkNfpBzx6vzOtIKXYzYU94+b6jSVzeElXnVwc9OqvOGAs9x87mCgBLhWy7EFh/V iM1qa7SRPfYvlm4eU2IWxUyh6FRtkD/BScBvEUetNS617+EyDOi4mhDOMv7Pjt89aEoo L1EEUz+AKSmK4niUHXMjoAIjdQEkzSJmqUQve8bhSGaa0/mp+AWmamfE43Eo3cNuQldg nrcTImWJ8kL2WN3tyabBsGY1OmiQlffYPss4k94g7bZrJSE/IbC5ZTg3x1Ju+uDGcgUx /eof63gz9SXzyiFp9KWmfar/0fBtxZrhXpHDs+V2WpcYZuqEc1ZP2XpICJ6Vyv0fUUq+ dbJw== X-Gm-Message-State: AOJu0YwY28z1Vvk3YN2HPXgJHdDvt0y7gBo8pYypEa9sIyrf4c9F1YwC E8fU0rQVuGtzOSZR1YMvCuSXhgExEvUc0gQbo7N5rGVa4A8NcOQZ X-Google-Smtp-Source: AGHT+IFjtCdiXDT6q9fEEQjv9CgLQkio/kzcSDQc//pXW2Hr2EgbtoT4elaBwQvcXiG/5W6Xg0apX83t6bbxIvTRd1Q= X-Received: by 2002:a05:6122:150a:b0:496:1f95:209a with SMTP id d10-20020a056122150a00b004961f95209amr3513504vkq.15.1698439327523; Fri, 27 Oct 2023 13:42:07 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a05:6124:cb:b0:387:f724:598b with HTTP; Fri, 27 Oct 2023 13:42:07 -0700 (PDT) In-Reply-To: References: Date: Fri, 27 Oct 2023 13:42:07 -0700 Message-ID: To: Mike Schinkel Cc: Larry Garfield , php internals Content-Type: multipart/alternative; boundary="000000000000ca2a0d0608b8b831" Subject: Re: Basic Type Alias From: oladoyinbov@gmail.com (Oladoyinbo Vincent) --000000000000ca2a0d0608b8b831 Content-Type: text/plain; charset="UTF-8" So, we have to discuss and conclude on the best syntax to use for the Type Alias: Syntax 1: ```php type T = int|string|null; ``` Syntax 2: ```php type T: int|string|null; ``` Syntax 3: ```php use int|string|null as T; ``` And also, we need a function to check of type exist, maybe `type_exists()` will be a great choice :). And also, in order to group and namespace the Type Alias, i suggest we use `typedef`, like i specify in my last message, it will be like this: File A.php: ```php namespace Package; typedef MyTypes { type Numeric: int|float|null; type Chars: string|null; type Result: bool|null; } ``` File B.php: ```php declare(strict_types=1); use Package\MyTypes; class Hello { use MyTypes; public function A(Numeric $id, Chars $message): Result { // get the value echo Numeric::value; // int|float|null } } ``` Well, it's just the round up of every features listed in this discussion, how is it? I will like to see your thought on this :). Best Regards O. Vincent. On Friday, October 27, 2023, Mike Schinkel wrote: > > On Oct 26, 2023, at 3:23 PM, Larry Garfield > wrote: > > > > App-wide aliases run into the autoloading problem. If the engine runs > across the add() function above, and "numeric" isn't defined, what can it > do? Currently, all it can do is trigger autoloading, which only works for > class-ish constructs (class, interface, trait, enum). Making type aliases > a full class-like construct seems... weird, and over-engineered. > > Curious, how do you define a "full class-like construct" in this context? > Anything more than autoloading? > > And would autoloading actually require something to be a "full class-like > construct," or could it not be something lighter weight (assuming full > class-like constructs are not than just autoloading?) > > > But if not, how do we autoload them? And if they do autoload somehow, > does that mean we end up with a bunch of files with a single `type` line in > them? That seems not-good. You also then have to ask how they interact > with namespaces. > > > Straw man proposal: > > When PHP recognizes a symbol in the context it would expect a type yet PHP > does not recognize that type, PHP could use the existing autoload mechanism > to determine files and directories in which those types might be located. > This would address namespaces, I believe > > Then, following PSR 4 is could attempt to autoload via a file of the same > name. If there are types and classes, interfaces, traits and/or enums with > that same name they would all need to be contained in that same named file, > much as it currently works. > > Of course that would require one .PHP file per type which as you mention > would not be ideal. > > So instead, a new PSR could be created to extend PSR 4 to add > type-specific considerations. For example, when the type passed in is "Foo" > it could first look for `/~types.php` and load it but it that did not work > then it could look for `/~types/Foo.php`. I picked the tilde (~) since it > cannot be part of a valid namespace of class name so no backward > compatibility concerns. > > If the type name passed to the autoloader is `Foo/Bar` then it could look > in `/~types.php` first, if not there then `/Foo/~types.php` and if not > there, then `/~types/Foo/Bar.php`. I picked the tilde (~) since it cannot > be part of a valid namespace of class name so no backward compatibility > concerns. > > Having a single `~types.php` per namespace would let developers put all > their types for that namespace there, if that is what they prefer. Or they > could pull all types in the root's `/~types.php`. Or they could create a > file for each, whichever their preferences. > > I am envisioning a new `type_exists()` function would be required. > > Even better would be to add an additional optional parameter $type to > my_custom_autoloader_new() which could either pass in a numeric for some > new predefined constants like PHP_AUTOLOAD_CLASS=1, > PHP_AUTOLOAD_INTERFACE=2, etc., or just a string like "class", "interface", > etc. > > Based on that idea, such an autoloader might look like this: > > function my_custom_autoloader_new( $name, $type ) { > $dir = __DIR__ . '/includes'; > switch ($type) { > case PHP_AUTOLOAD_TYPE: > $file = sprintf("%s/~types.php", $dir); > if ( file_exists( $file ) ) { > require_once $file; > } > if (type_exists($name)) { > return; > } > $file = sprintf( "%s/~types/%s.php", $dir, $name ); > default: > $file = sprintf("%s/%s.php", $dir, $name); > } > if ( file_exists( $file ) ) { > require_once $file; > } > } > > > So any type alias proposal would need to sort out the above "definition > problem" in a way that's generally acceptable. That's been the hold up for > 3 years, I think. > > The above is potentially one way to skin that cat. > > Anyway, I'm sure there will be lots of opinions on this, so bikeshed away. > > -Mike > > --000000000000ca2a0d0608b8b831--