Re: [RFC] [Discussion] Never parameters

From: Date: Mon, 10 Mar 2025 22:59:49 +0000
Subject: Re: [RFC] [Discussion] Never parameters
References: 1  Groups: php.internals 
Request: Send a blank email to [email protected] to get a copy of this message
On Mon, Mar 10, 2025, at 20:05, Daniel Scherzer wrote:
> Hi internals,
> 
> I'd like to start discussion on a new RFC about allowing never for parameter
> types when declaring a method.
> 
> * RFC: https://wiki.php.net/rfc/never-parameters-v2
> * Implementation: https://github.com/php/php-src/pull/18016
> 
> -Daniel

Hey Daniel,

This looks interesting. I'm not sure that I like "never" as a parameter type and
while it "technically" doesn't violate LSP, it seems like a backdoor to doing just
that:

abstract class Point {
  function add(never $other);
  function subtract(never $other);
}

class Vector2 extends Point {
  public function add(Banana $other) {}
  public function subtract(Football $other) {}
}

There's basically only a gentleman's agreement that a subclass will implement things in a
way that makes sense. I would also personally prefer associated types:

abstract class Point {
  public type OtherPoint;
  public function add(OtherPoint $other);
  pubic function subtract(OtherPoint $other);
}

This at least lets you ensure the "other point" is the same type in both functions, though
personally, I'd rather just have generics.

— Rob


Thread (33 messages)