Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:119451 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 27548 invoked from network); 2 Feb 2023 14:19:56 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 2 Feb 2023 14:19:56 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id E38831804BD for ; Thu, 2 Feb 2023 06:19:55 -0800 (PST) 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.2 required=5.0 tests=BAYES_20,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM, 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-f169.google.com (mail-vk1-f169.google.com [209.85.221.169]) (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 ; Thu, 2 Feb 2023 06:19:52 -0800 (PST) Received: by mail-vk1-f169.google.com with SMTP id t190so925030vkb.13 for ; Thu, 02 Feb 2023 06:19:52 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=to:subject:message-id:date:from:mime-version:from:to:cc:subject :date:message-id:reply-to; bh=BE96CNL1Er9fNS9i85UDZj3k/T0TXb4uWrzf1QBkX50=; b=Ajt3HibFQybYMASjoLg+I7vBp4btiIVvGyfUje+cgxR6M3MJBKUYnIPzfi0hFWYKMz GiYgNWWq5rf0YXW/c10ozMES7Tb6n1nHfia2hkI5gexIVZR6yTMQ+q2UkeQz9QjmkYg6 jDN6ZH17jNnhpTvz5Qgb9P9sLe6i5QoemVWgQmBOUZ3PpZc7RNg+CMulTkWVkZz3wuWC aOGGIo9XakvlrO1XYYgtE7C8PBL6mkgPgxoJOjIPoZ8iZcSR/pfhMKqvAmzuy6LzqJme Xzmlb3+yDJCThmIf/tJ1jH9LDMOl0gPyA5/3WM9GAY8rh+XQNPGrL2bflH47RF0AdU9s jqUQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=to:subject:message-id:date:from:mime-version:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=BE96CNL1Er9fNS9i85UDZj3k/T0TXb4uWrzf1QBkX50=; b=F8FnbCxfzQyb0UJjolnqmDjHSBLHsS9BY+Tn1a108gzfPD6CJYNfX1snGLLOVFCFk2 OE/ywgnTMMIrfff+wPI6wCPvlIfayV18qfSukdEIWlDBc7OrJIHI45IQ6N7b/iHtposG OAHdQ9VxKhRq5z0pcgyG4LfHVyewWC6QRKYpX9XmSI02jvo0Hn46YEqg95KUa2Vdn1wo QOOVAO+jcU5PbIf1vjZ3QKMqob0A4aor62sAJZGEyhtNBYpkA/nMkIm7CS3PtDHyz0Fw kATRIUn+UGWr+NKDAvha27hxe3BIRx3joiu8U3rVsP6/yVyNaZePir3v8DD6JfzIH0A3 39yw== X-Gm-Message-State: AO0yUKV5mcVG+qCQ9Ck25O7FOK+mo7E2dimh2o5baMJOqCpEVpd1/KiI puZmql2tK4iBqc56oysLQjpAU2afMaLA9wIQ7L4DqSOZWZB1Yg== X-Google-Smtp-Source: AK7set8OgQ1LJ6oOuyf4Vvg8fciB4KERGwGOWEA7siJzaVqq2y7HBUSWO6b7GA12hTS8wQxIUcZPt5yrP8K/Fv2nmHI= X-Received: by 2002:a05:6122:b48:b0:3e7:ec3e:4355 with SMTP id 8-20020a0561220b4800b003e7ec3e4355mr969045vko.15.1675347591689; Thu, 02 Feb 2023 06:19:51 -0800 (PST) MIME-Version: 1.0 Date: Thu, 2 Feb 2023 16:19:40 +0200 Message-ID: To: php internals Content-Type: text/plain; charset="UTF-8" Subject: Official Preprocessor From: someniatko@gmail.com (someniatko) Hi Internals The main gist: -------------- I suggest to introduce an official type-checker / static analyser / preprocessor / transpiler for PHP, somewhat similar to TypeScript in the JavaScript world, which will allow to introduce the features otherwise impossible for the PHP world, at the cost of including an additional transpilation step. The reasoning: -------------- You all know there are a lot of problems with the language, especially large gaps with the current type system, which have large community demand to overcome, but still cannot be solved due to the paradigm chosen by PHP. And the paradigm is to be statically typed (to some extent), but dynamically checked language, which limits the number of available options due to performance reasons. The introduction of static types have gradually eliminated the need of using external means of communicating the types (phpdoc @param and @return annotations), replacing them with native language constructs, making the language more expressive and enterprise-ready. However, there are a lot of things still missing, like types of arrays, including lists, hash-maps and structure-like arrays, and of course generic classes and functions. These are still covered nowadays with external means like phpdoc annotations and static analysers / type checkers to validate them. This leaves PHP in the odd mixture of native types and externally validated annotations via comments. Paradigm of a dynamically checked language also leaves out the problem of type-checking before running the code. Even though the types are defined statically, you still have to physically run the code, with all possible execution paths, to catch a type error. To avoid that you still would have to use an external typechecker / static analyser, even if PHP could cover 100% of type demand natively. Python solves this problem nicely I think. It is dynamically typed, but it allows a special syntax for static types and also has a separate static type checker. I think this is really a good approach and it would be great if PHP followed this path for the beginning, but we have what we have already. There were attempts to create preprocessors for PHP (like PHP++ IIRC), which allowed features like generics, but they did not gain enough popularity, weren't baked by large enough companies, didn't make their way to major IDEs support. I believe the only solution is to make an official one. Therefore, I suggest the following thing: 1. Introduce a new #declare directive which will mark the file as requiring pre-processing. 2. Let PHP introduce special features that only work in such "pre-processing required" files, such as typed lists and generics. 3. Implement a static type-checker in PHP that will verify those typed lists and generics are indeed used in a valid way. 4. Make the PHP preprocessor output the "normal" PHP file somewhere, which has list types and generics erased. I think something like that was suggested earlier many times. As an alternative to a new #declare feature + validation + transpilation in "plain old PHP", the Python way of just ignoring statically checked stuff could be applied. This approach though looks odd to me in PHP world, because having some stuff checked in runtime, and some only statically may confuse developers a lot. Basically, this will split PHP in two languages, one being superset of other, but checked statically. I realize this might be much of a paradigm shift, it may require lots of work if accepted (I can only dream of it though), but still I want to share my thoughts with you and propose something for consideration. Regards, Illia / someniatko