Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:117729 Return-Path: Delivered-To: mailing list internals@lists.php.net Received: (qmail 89016 invoked from network); 13 May 2022 13:10:03 -0000 Received: from unknown (HELO php-smtp4.php.net) (45.112.84.5) by pb1.pair.com with SMTP; 13 May 2022 13:10:03 -0000 Received: from php-smtp4.php.net (localhost [127.0.0.1]) by php-smtp4.php.net (Postfix) with ESMTP id B7E3D18005C for ; Fri, 13 May 2022 07:49:33 -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_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-yw1-f181.google.com (mail-yw1-f181.google.com [209.85.128.181]) (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 ; Fri, 13 May 2022 07:49:33 -0700 (PDT) Received: by mail-yw1-f181.google.com with SMTP id 00721157ae682-2f7bb893309so92429897b3.12 for ; Fri, 13 May 2022 07:49:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=sSAnMZLjUUvXlkDCVZWOipd4pM6mdSzNuNwRJT6mHoc=; b=bHfWo5vYNSuRBRibpeIljW9sinNgzgylWAMHsWXobrUIBcxnbrDcZoJzdJwe7iMpBy /Q3QI8JC8Ni896ph0CXNB5VrxYtJgVopH2VxZX0MI7UDJXlul4mizQPPBnieGvYQ7Vy0 RPm4Xq9j+dYnTfbKRIz4KwjdGV2/rwlJwycHeeP6zzmi7bUUQKUD0IRoAfgqewJSba6+ eE8dA0phBMJAAUepn0tmSf0PrNLwOldr4X30kE3r0j4cDG1ynsEh39BBsy+mqeuvpF1d 3Pht5C9JMwB/HmtfLolo8AzbF+z2uNUSR3DF2aKgMOyRCd/PfA8Caxjv613IQGAGIYsv ZLnQ== 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; bh=sSAnMZLjUUvXlkDCVZWOipd4pM6mdSzNuNwRJT6mHoc=; b=XNiHE0CrRQr56m+A+fs6+xlpwDUrcXQ+DVKMcFxKOGGWq5AMVfdWqso1zXeZuJ97Pz 4LD3+Yty3I5xhEx/IL3V98zcN36yrXdRAQMpL0dU4WLZLSFiMFSj/Zcryt1wODSPTa8X Kszq4Uo5CwwhYPQhr3HejAl9YBxRdWfnDvja+0fh9JJKJr0vKAsdXZNTb0KxLOGXBeCv dVZ4JLGe8Wfw9+hEdRjANsUmQ5WtTJ+KOROI+6v0NUmJz/u6uDb+CTXCDZnEzXcl48sx kRNswyb6poYVCQVr4X29u6v/SaqKOEssMun4DaWTlaepcSrVjxSSdWhT3pBGf+P+7x7U DdDQ== X-Gm-Message-State: AOAM530ze4NAPxBIB3NlNUeWFghMXRReN/6w/ZSm1JInThvwRjpDM8Ek VMFAlzHgPq4U28C71zu+CemU15XxZkxCzxK5a/dEMRNu X-Google-Smtp-Source: ABdhPJzxEMbUN+b4XLsnMW+jI0I8ck5goWE6wLWTQSXclTVSOM0j3PSXorVHG04VZzUwTRla4Jo1QQhscqnwyyFUFJA= X-Received: by 2002:a81:cf02:0:b0:2d0:b68c:cf30 with SMTP id u2-20020a81cf02000000b002d0b68ccf30mr6384768ywi.510.1652453372605; Fri, 13 May 2022 07:49:32 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: Date: Fri, 13 May 2022 07:49:20 -0700 Message-ID: To: Rowan Tommins Cc: PHP internals Content-Type: multipart/alternative; boundary="000000000000483a4005dee5c876" Subject: Re: [PHP-DEV] The future of objects and operators From: jordan.ledoux@gmail.com (Jordan LeDoux) --000000000000483a4005dee5c876 Content-Type: text/plain; charset="UTF-8" On Fri, May 13, 2022 at 7:05 AM Rowan Tommins wrote: > > I like Larry's "4 levels", but I've been thinking that there's some > existing functionality in PHP which takes a different direction: rather > than overloading *operators*, the language lets you overload *behaviour*. > We have magic methods like __get and __call, and interfaces like Iterator, > Countable, and ArrayAccess. Some hook into a particular function, or even > operator, but they're generally described in terms of what they do, not > what they look like, if you see what I mean. > > From that point of view, overloading comparison and equality *behaviour* > makes sense - it could affect not just the == and <=> operators, but things > like in_array() and sort(). I think this distinction is more pronounced in > PHP than some languages, because the standard library isn't "self-hosted": > a sort() call doesn't literally compile to a call to $a <=> $b > > I have been thinking about something similar, but not in the context of making operator overloads more like behavior overloads. Rather, I've been considering the idea that operator overloads are a *subset* of *engine overloads*. Ways that the developer can provide additional details to the engine about the behavior of their code that allows the engine to make more concrete assumptions about how it should be processed and interpreted. I started thinking about this mainly from the perspective of the syntax. I proposed the `operator +()` syntax in my overloads RFC, but my most compelling argument about the reason for it was sort of obscured because it was wrapped up in only overloads. To that end, I was considering more broadly what *all* magic methods are on objects: handlers. In fact, in the Zend engine that's how many of the same sorts of behaviors are described, as object handlers. Most of the standard library functions don't make calls for such handlers. For instance, sort() makes a call to zend_compare, and zend_compare ends up making a call to the compare handler on the relevant object. So I was at least considering the idea of a more broad replacement of the syntax for object behaviors, just not for an RFC related to overloads... such an RFC would have its own difficulties and controversy, and would almost certainly require a period of dual syntax support for old magic method syntax, making the arguments against very easy while the benefits would be more long term and less immediately apparent. > It's less obvious how that applies to mathematical operators - should > implementing "addition" allow an object to be used with array_sum() > perhaps? And what about deriving one operation from another, e.g. $foo*3 > running $foo+$foo+$foo, or $foo**3 running $foo*$foo*$foo? > > I don't really have a conclusion here, I just wanted to throw it out there > as a different mental model to consider. > > This would only be true for numerics, but not other kinds of math, such as matrices. Mathematical operators really are something that require direct calls and direct overloads if they are supported in any way, unless the language is willing to essentially never have things like complex numbers, matrices, etc. even in extensions. Still, it's an interesting thought and definitely the kind of high-level discussion I was looking for. Jordan --000000000000483a4005dee5c876--