Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116231 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 26442 invoked from network); 5 Oct 2021 17:03:01 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 5 Oct 2021 17:03:01 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 4E43A180541 for ; Tue, 5 Oct 2021 10:47:32 -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=0.0 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_NONE 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-oi1-f180.google.com (mail-oi1-f180.google.com [209.85.167.180]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by php-smtp4.php.net (Postfix) with ESMTPS for ; Tue, 5 Oct 2021 10:47:31 -0700 (PDT) Received: by mail-oi1-f180.google.com with SMTP id w206so337735oiw.4 for ; Tue, 05 Oct 2021 10:47:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=newclarity-net.20210112.gappssmtp.com; s=20210112; h=from:content-transfer-encoding:mime-version:subject:message-id:date :to; bh=ukBcU9B3TulbrQCjFJECgbfjOwPUfrt2q8gIz0OCLro=; b=v9VXpUmTfDahPlHuwEaul5xM0ZeJCdgxdlldFmIROHAuLh5J87DWcruMphIW6FJ0Ot +DAev+SZ4uSL3BYGPcmfUvJYNZ2D17sYEeRHmaCMyizvkv3UTEzoyZgiyiaAI0++G6cW 5/6Z+aIfab+70Ff10LLz+3HH+QqkvzD4sYrekW0Sfe5NBfR5dH5y93kO7zFjSZtWdvda 4/fuPqdmw9nuuq2+hmEehFTGleC4WhjLb8hlEwFUO5b3xG4AzlgJeWxKccZlaXyu+vib VBtGpBFKUAqoY88I8Ofc1xGf+yj82x/n1pI125pPANJQRug1rx+wr1855JaRU71tShuH S9ag== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:content-transfer-encoding:mime-version :subject:message-id:date:to; bh=ukBcU9B3TulbrQCjFJECgbfjOwPUfrt2q8gIz0OCLro=; b=aQ3QgYHQNvgjpWWF4A7y/ORZkXlBVenKmApUhuX8vTTWgH2KsQKjX63nx/mz+dHasN ty4UeNvnSaVcaUeSgbR2ITUc6Y2DnBtAnq2agL4Z/nMtbAYXSNl4rsAysWfit+PsVCkz 2I3Xa/hN8G33+9MqQuI/YLQ41gRxLZbbMhTQDLBXlW2BHqah2l4ocpBgLsMdQCg/21wv oGIviB+hl94vFM1gJe5BXFl2NlKZa9UnvajyTBRGWGwGNy8/6v0BKGCThl5RMUu2yHLb OKXt+TH7/u+Hf1iWXBDF7AC23Dcl/3lYZoRNSclz+5pFd9gETV7g18KeDaG2JJlCHkbq CAFw== X-Gm-Message-State: AOAM530C1u7elaK7ZeD1TE2Xe1KGxEqH4X8jhTVsXfaKLZcSkEYC29J5 OHu0wLhPtG+hkI7s+mU867t7As5tv57stA== X-Google-Smtp-Source: ABdhPJyG7qrrJ4tJHQ1NDmGL/nI7sFPHEpjZY3ksKsXd6lfAb7i3VngSy/cTKJ4E4vAlh5iIhoatzw== X-Received: by 2002:aca:2106:: with SMTP id 6mr3666067oiz.149.1633456048499; Tue, 05 Oct 2021 10:47:28 -0700 (PDT) Received: from [192.168.1.10] (c-24-98-254-8.hsd1.ga.comcast.net. [24.98.254.8]) by smtp.gmail.com with ESMTPSA id h18sm3654155otg.2.2021.10.05.10.47.28 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Tue, 05 Oct 2021 10:47:28 -0700 (PDT) Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Mime-Version: 1.0 (Mac OS X Mail 13.4 \(3608.120.23.2.7\)) Message-ID: <29103CC9-22A5-4507-A982-CFFC20450435@newclarity.net> Date: Tue, 5 Oct 2021 13:47:27 -0400 To: PHP Internals List X-Mailer: Apple Mail (2.3608.120.23.2.7) Subject: Function list declaration syntax? From: mike@newclarity.net (Mike Schinkel) Hi all, This email proposes a shorthand syntax for declaring a list of = single-expression functions.=20 A similar albeit more verbose attempt to provide short functions[1] = failed late last year, but I was recently inspired by Sebastian = Bergmann's Type library[2] that he referenced in a comment on another = thread. Consider the `Type` class[3] in the `SebastianBergmann\Type` namespace. = It contains 60 lines of function declaration to define 14 functions that = all return `false`. Even though it really is a simple class it is = rather hard to wrap one's head around quickly because of all the = bloating boilerplate required. At least IMO. Consider instead if we had the following syntax? public function list: bool { isCallable() =3D> false, isFalse() =3D> false, isGenericObject() =3D> false, isIntersection() =3D> false, isIterable() =3D> false, isMixed() =3D> false, isNever() =3D> false, isNull() =3D> false, isObject() =3D> false, isSimple() =3D> false, isStatic() =3D> false, isUnion() =3D> false, isUnknown() =3D> false, isVoid() =3D> false, } The idea here is to group a 'list' of functions =E2=80=94 in this case = of the same type =E2=80=94 and then use shortened single expression fn() = syntax for each function. And with a monospace font in your editor of = choice this code could be made even easier to grok by left-aligning the = return values. I chose the keyword `list` because it is already a reserved word, but = the word `case` could also be used for the same reason, if people prefer = that. Further, instead of `function list` or `function case` we could have `fn = list`, `fn case`, `fn() list` or `fn() case`, whichever feels best to = the most people. This could also work for abstract functions noting that the typehint is = applied to each function instead of the list declaration, e.g.: abstract public function list=20 { isAssignable(self $other): bool; name(): string; allowsNull(): bool; } Those two changes shorten Type.php from 140 lines down to 90 lines, and = IMO, make the class much easier to read. You can see it here[4]. One more example, this time his `NullType` class[5] using mixed type = hints and going from a total of 38 lines down to 26 lines[6]: public function list { name(): string =3D> 'null', asString(): string =3D> 'null', allowsNull(): bool =3D> true, isNull(): bool =3D> true, } What do you think? Is this approach any more appealing than the last = one proposed? -Mike [1] https://wiki.php.net/rfc/short-functions [2] https://github.com/sebastianbergmann/type [3] = https://github.com/sebastianbergmann/type/blob/master/src/type/Type.php [4] = https://gist.github.com/mikeschinkel/6f8c9cf6700a75e75e2725db1ed4da61 [5] = https://github.com/sebastianbergmann/type/blob/master/src/type/NullType.ph= p [6] = https://gist.github.com/mikeschinkel/a93aabd61127b0c3ba28c5caf4ebe005=