Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:116789 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 22668 invoked from network); 3 Jan 2022 17:11:43 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 3 Jan 2022 17:11:43 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id 21E2018053E for ; Mon, 3 Jan 2022 10:18:43 -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=-1.9 required=5.0 tests=BAYES_00,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-ua1-f45.google.com (mail-ua1-f45.google.com [209.85.222.45]) (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 ; Mon, 3 Jan 2022 10:18:42 -0800 (PST) Received: by mail-ua1-f45.google.com with SMTP id u6so51755615uaq.0 for ; Mon, 03 Jan 2022 10:18:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=dqxtech-net.20210112.gappssmtp.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=41z1Wo2i8B6cLlKjzLNoA8p5lWwsI0y+tQj/WH58cTA=; b=tJhwnZ6HVTsYAqChU5derkHMLtq6Q03foguZhMoKYNO7296rWZgxHbYHrTNTEZTMvu uFw4pZRsMr+XeDEFBYaC5JqL/jqHVyldbsRTBZ0qEtY2z3R7q36e9tMWyCsdKwYoWvGn CFYj3+1/C42fJ2v0+iGVdOe5igwIt3WWh1caByaVLHBF+9g3RJ+DgetbbWwwEYPI44IQ QQvdbp9FItdiN+FJfUj4xkHj4zazTySh7jJh8tJJToffDj2pIKzlqelkXSY+o64H8zwd M/dgTZtOFzJ7PW5mqx6DgfA7JpabRKiVvzZABD3ivCQzFP1e3j1OVWNXEYN+qwjhqs+D go5A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=41z1Wo2i8B6cLlKjzLNoA8p5lWwsI0y+tQj/WH58cTA=; b=m2sLgOWZR3bLlkz7rbEzPJfAOvLSCTS7yKq6lECpOESq0G1OU7dRbWYzts2ay/3SWn 0yi+179x5yO1Md4gjsGPYXj3OJVcptL10rYj8zLeO6y7hh56HRMREvyOYJWbNUzG97AI vmnnyqVeAoVfGcSohllKg7EZRJWnGfO/e/5yt7qEIEzzAkO4qsXi+J1g4V6RIGO8Sj4h aU8ZgImEieZ10CKCrXw4Eo79Qv8EfF09OmAUKzNRsUCGemXRY8f8jg7EGiW0Q5LIx57P pnzeu5ZirXuBLRJMBi1WC1ZP39KvtzZNG7xm1ApSCxZ9S3etxXyd5tRAvwREr+hkZ7o9 bwPA== X-Gm-Message-State: AOAM532lawE8zCYvmMqup6znrgk3P5CYEk+PLONOHkiBUXIXj5GPYlXQ XrnpMefw+LEHD0urau0LVVCIRNW7HvOcgunmgP1eTw== X-Google-Smtp-Source: ABdhPJwugGxymUl8ZJKSVuBadkadoon/9UllnnR9hQT6WI8xqYpZgkxO04JHtBu9I608OUUP0oAtR3Q+e7o7TfEvQcQ= X-Received: by 2002:a05:6102:736:: with SMTP id u22mr14173369vsg.60.1641233921994; Mon, 03 Jan 2022 10:18:41 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: Date: Mon, 3 Jan 2022 19:18:30 +0100 Message-ID: To: Marco Pivetta Cc: Jordan LeDoux , PHP internals Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] [VOTE] User Defined Operator Overloads From: andreas@dqxtech.net (Andreas Hennings) On Mon, 3 Jan 2022 at 16:00, Marco Pivetta wrote: > > Hey Andreas, > > On Mon, Jan 3, 2022 at 3:40 PM Andreas Hennings wro= te: >> >> I imagine that operator overloads can improve DX for these use cases, >> but at a cost for the overall language. >> >> How would you (Marco) see the future of arithmetic libraries for time, >> money etc without overloaded operators? >> How would these calculations look like e.g. with infix functions? >> Do you think this can eliminate the desire for operator overloads? >> >> >> E.g. something like this? >> >> $ts0 =3D new Timestamp($seconds0); >> $ts1 =3D new Timestamp($seconds1); >> /** @var Duration $duration */ >> $duration =3D $ts1 - $ts0; // Operator overload notation. >> $duration =3D Duration::betweenTimestamps($ts0, $ts1); // Static method= notation. >> $duration =3D $ts1->diff($ts0); // Object method notation. >> $duration =3D $ts0 $ts1 // Infix notation >> based on static method. >> $duration =3D $ts1 $ts0 // Infix notation based on object method= . > > > I'd probably use `$ts1->subtract($ts0)`, which doesn't seem to be that un= readable, and there is no need to abbreviate it to "diff" either. > Whether it needs to be static methods or instance methods depends on the = wished API design. Ok for "subtract", I should have thought of that :) Although for me "subtract" sounds like a possibly mutable method, whereas "diff" sounds immutable. This said, I am not really convinced that the current syntax is "good enoug= h". > > What this RFC aims at is a mathematical language, inside another general = purpose language: for complex expressions, I'd probably run it in a subsyst= em dedicated to this instead. For isolated "complex expressions", perhaps. But we should think about algorithms with multiple small math operations, e.g. a foreach with a repeated calculations, some of them in conditional branches, e.g. to calculate prices, taxes, rent etc. In all these operations, we want static analysis to check compatibility of operands. I think these algorithms will become more readable with overloaded operators, while any "embedded formula engine" would make them vastly more complex. Btw others already pointed out that we also want comparison of value objects, not just math. But I came up with the math use cases, so am sticking to it to be fair. Personally I still don't have a clear opinion for or against, but I also don't have voting rights, so... > > See for example: > > * https://en.wikipedia.org/wiki/Expression_(mathematics) (I'm literally = just picking a complex example - don't even know how to read that properly) > * and its textual representation in https://en.wikipedia.org/w/index.php= ?title=3DExpression_(mathematics)&action=3Dedit§ion=3D1 > > ```php > $expression =3D <<<'MATH' > f(a)+\sum_{k=3D1}^n\left.\frac{1}{k!}\frac{d^k}{dt^k}\right|_{t=3D0}f(u(t= )) + \int_0^1 \frac{(1-t)^n }{n!} \frac{d^{n+1}}{dt^{n+1}} f(u(t))\, dt. > MATH; > > $result =3D $userlandExpressionEngine->evaluate( > $expression, > [ > // ... bind parameters here .... > ] > ); > ``` > > Heck, I can probably ask the `$userlandExpressionEngine` to render that m= onstrosity for me (see attachment). > > Note that we do this stuff constantly for SQL, yet we haven't designed a = system for embedding SQL into the language, Actually we do have query builders, to reduce the amount of literal SQL strings in code, and to make it feel more "native". And where we do have literal SQL, we have to be very careful not to break things, especially when binding or escaping/encoding variables. > and still, SQL is used many magnitudes more than all what was discussed i= n this RFC. This may be true currently, for the arithmetics use cases. This said, the reason for a small amount of math libraries in PHP (which I did not actually count) could be exactly because of a lack of overloaded operators. And I think timestamp calculations are already relevant enough in today's c= ode. It would be interesting to see how much overloaded operators are used in Python, and whether the good outweighs the bad. Unfortunately I don't really know where to start looking. > > Yes, strings are problematic to some degree, but it's still better than i= ncreasing language complexity for a very edge case. > > Alternatively, a better way to embed other languages can be used, which w= ould be much more useful for things like Twig, Blade, PHPTal, SQL, etc: In = haskell, this is done via Template Haskell, which many like, and many loath= e, but is still useful for type-safe operations with a different language t= han the "main" one: https://wiki.haskell.org/A_practical_Template_Haskell_T= utorial#Shakespearean_Templates > > In addition to all the above, I just noticed that the entire reflection A= PI in the RFC requires major BC breaks in the reflection API... sigh. Interesting. What exactly is going to break BC? > > Marco Pivetta > > http://twitter.com/Ocramius > > http://ocramius.github.com/ >