Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:125481 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 A29891A00CB for ; Mon, 9 Sep 2024 18:41:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=php.net; s=mail; t=1725907405; bh=zOlMg6QE53qKwKd2i9VEjw/6T3INVlp70sthDRsco8s=; h=Subject:From:In-Reply-To:Date:Cc:References:To:From; b=FSfzZ/HwIObnu++2KK0+S8VJAMyQtiATho/NydYXCPB8Zc3B6b6Cyqura0huTFSr3 L02F7GisMGVE5TnSFc9xtJP3jyrWx0Q9wpxPclpIdZZIIimfmeky7YdJ4b2pP8DS9+ AicZCjuzSvwihwtStXSWsRazO98t91t5QVdLpr6qPqeM74vGEzGbieiL3wyfv6Iz5E IjBDUlZObeY03vIDnwjWWzYrZkWhdRxefp4O7TsbTt2UoRfInDB9FvQG6Lc1MXTAf4 AIaTHBHX+21TAqVtB62v/COiVuJimpjI8bRyy4BPGPMDix2XaA4J8MNSB7zZ6ow/5p ASgBxsFy4dUFg== Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id CBB6E1801E5 for ; Mon, 9 Sep 2024 18:43:22 +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_DNSWL_NONE,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-yb1-f180.google.com (mail-yb1-f180.google.com [209.85.219.180]) (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, 9 Sep 2024 18:43:20 +0000 (UTC) Received: by mail-yb1-f180.google.com with SMTP id 3f1490d57ef6-e1a9dc3f0a3so5162104276.0 for ; Mon, 09 Sep 2024 11:41:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20230601.gappssmtp.com; s=20230601; t=1725907279; x=1726512079; 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=VrF3J1ayEZHRjUb+TIrNmJEoLJxEzmUl+ElyL52bH58=; b=pNncHd+rchQ6XvHaejtNwyrW5Q+jotXs1A7XeBJndaXDnePYJu6IoQKQpeGfLnvXx7 P0VwtfHjiknt7H4QJaCxWtZIDdWRD/nz3g/hRrlu6tyIhFBSKRUu/r6TlXuWR8JGzRLk 14wRkmbNTipvxWn1wakv+970Iw5yAWWAt/Tsj+dBvKUoWGeG8k3uNiCRCAhGfeOd5JIF r9HcW7psaANG+YpRa3Nq72Q6HoIJX24h1eMFjsroqFpwdejLMn/tQiptRcHdAiX1KUae BAGBvyFKmGdbES5CG8BwamvRINBUJlVo3niepXjT7cUaPDHVhdCw+2QyOrQJL25l/0Xl Tvfg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1725907279; x=1726512079; 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=VrF3J1ayEZHRjUb+TIrNmJEoLJxEzmUl+ElyL52bH58=; b=fBP0XeB1+dnHN0sOfdITC6m3RktY2/VPhMEDtdETqGBF9rtnQSAVZSpvWXk6lwHNj0 7042KuEGjR5WB4YVAoMZfjAguiGutbmLqq5jkvjf7RXOHEyH/7LpSReODwYsaNeQ0sm8 RtDqgz8x+IcxshcUMI4myfX1CiG9X8sneeWtrjY0E8k1GrQisBZ0IKXUBQpmUcahP4ET OH25EyH48XkwGAwUs5jXK1MY+4TF3Xi5cWyV690Joc7OYp1N0p4yKWNrJWA03njX6vLM HdbLJY4bK0R3ETvkwU62HLWoYZhIKm14csNAvRLoJleMqUJkjbsygavoq23r1Qk11Tmp BW4g== X-Gm-Message-State: AOJu0YytkhQ8mDO3hHaecc2WIy4OqjFxjKRkLNTjNXViS93TMC+1i7n4 3QZeNXFWm/oRZHCF5JhASU6W6G16FeiOle/B1TwcU1FVRf9EIMKENiHzSfOExQmMSoH9qWQS3oa uYCI= X-Google-Smtp-Source: AGHT+IHHgMPQMOqKDWp7/tN1oSW8tpz9i5eMA4u+r3+Yh1K5b6xVZNsxaVpUtWPzhK7NTBc7PnjJ4A== X-Received: by 2002:a05:6902:2808:b0:e1b:27d7:76bc with SMTP id 3f1490d57ef6-e1d349c38e9mr13752498276.31.1725907278606; Mon, 09 Sep 2024 11:41:18 -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 3f1490d57ef6-e1d7b9db6c2sm21561276.9.2024.09.09.11.41.16 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Mon, 09 Sep 2024 11:41:16 -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.10\)) Subject: Re: [PHP-DEV] bikeshed: Typed Aliases In-Reply-To: <63d241a8-a34a-498c-a5f9-f34230aa5afa@app.fastmail.com> Date: Mon, 9 Sep 2024 14:41:16 -0400 Cc: php internals Content-Transfer-Encoding: quoted-printable Message-ID: <4C7A7F27-B787-44CA-B664-CEF4B9B412FB@newclarity.net> References: <0fa39535-f22d-4eba-b4df-90abe39e683a@app.fastmail.com> <79e58673-50ec-461e-a998-736b020e4287@app.fastmail.com> <928A2984-6035-4DA6-9EA7-12E85237C270@php.net> <0d461700-1b6c-44fd-9cda-aa698de49847@app.fastmail.com> <667233C2-BC47-4530-8142-D90E6907FE63@daveyshafik.com> <63d241a8-a34a-498c-a5f9-f34230aa5afa@app.fastmail.com> To: Larry Garfield X-Mailer: Apple Mail (2.3696.120.41.1.10) From: mike@newclarity.net (Mike Schinkel) > On Sep 7, 2024, at 4:36 PM, Larry Garfield = wrote: >=20 > The other direction is: >=20 > typedef UserId: int; >=20 > UserID is now an object that consists of just an int, but can be type = checked against. What's unclear is if you can do other int-y things to = them (add, subtract, etc.), or if it's really just a shorthand for Referencing prior art (e.g. Go) PHP could allow int literals =E2=80=94 = e.g. `1`, `47`, etc. =E2=80=94 to be passed to typedefs derived from = ints but require int variables to be typecast to the required type. Same = for string literals. =20 In Go you cannot add or subtract on a typedef without casting to the = underlying type. I would definitely prefer that to be relaxed, but only = if it is relaxed via an explicit opt-in, e.g. something maybe like this: typedef UserId: int operations: +, -, *, /; typedef UserName: string operations: .; Or less cryptic: typedef UserId: int operations: add, subtract, multiply, divide; typedef UserName: string operations: concat; Going with the named operations would allow other operations to be = opt-in in the future, but would call into question defining operations = as a first-class language element. > readonly class UserId { > public function __construct(public int $value) {} > } >=20 > I could see an argument for either. =20 Typedefs enable a developer to write more robust code that they = currently cannot do, whereas typealiases are really just syntax sugar, = albeit sugar that probably tastes really good. Said more explicitly, I would prefer both but if it is has to be only = one to start, I would prefer starting with typedefs. > Though that opens up all kinds of interesting questions about a = typedef based on another typedef, if that's a form of inheritance or = not, etc. Again, I'm not sure if Rob wants to go there or not, but it's = a place my brain has gone before. :-) Given that a typedef can always just reference the original type(s) = rather than basing a typedef on another typedef I would err on the = conservative side and say initially no typedef of a typedef.=20 The current downside would be that a complex union typedef defined in = one namespace could not easily be referred to in another namespace = without repeating the union typedef. Whether that would become a = frequent problem remains to be seen so it could wait for a future RFC if = so. Another limit would to the workaround would be if a typedef is defined = as private for a namespace. This as namespace-private is not currently = possible we could cross that typedef-on-a-typedef bridge on a future day = if namespace-private ever becomes possible. #jmtcw > We may want to focus just on aliases for now, but design them in such = a way that they do not cause an issue for typedefs in the future. (Eg, = using the `typealias` keyword instead of just `type`.) Another option is to use a different syntax: type MyNewType: Foo type MyAlias =3D Foo Not arguing for or against any specific syntax, just pointing out that = there are other potential options. -Mike=