On Sun, Jul 11, 2021 at 5:19 PM Larry Garfield <la...@garfieldtech.com> wrote: > > On Sun, Jul 11, 2021, at 5:30 PM, Levi Morrison via internals wrote: > > I like Rust's From and TryFrom traits, which allow types to define > > conversions into or from other types. The From trait always succeeds, > > and TryFrom can fail. However, it's not a "cast" -- the user calls an > > `into`/`try_into` or `from`/`try_from` methods. > > > > I would be supportive of defining official ways to convert types from > > one to another, not necessarily casts, though. I think these qualities > > are important: > > > > 1. Would have failable and non-failable variants. It's nice to know > > that a particular conversion cannot or can fail. > > 2. It should be amenable to static analysis. Tools should be able to > > warn about missing paths for handling failable conversions, or that > > conversion from A to B is possible but A to C is not. > > > > I'm not sure how to technically achieve this for PHP. I don't think > > any casting proposal I've seen meets these characteristics, so I would > > personally vote no on them. > > What are the use cases for integrated object to object (ie, class to class) > conversion in PHP? Rust's type system is a very different beast, so its > casting logic makes more sense there. I'm not entirely clear on what the use > case is in PHP. When would that be superior to "just write an asFoo() method > and move on with life"? > > Scalar conversion I can see, but every time someone suggests adding siblings > to __toString(), there's major pushback. > > --Larry Garfield > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: https://www.php.net/unsub.php >
The features I was referring to are not casting; casting does exist in Rust but this is not it. Rather, it's a generic way to convert objects. It's nice to have a standard way to do it, instead of every project doing something bespoke. It also can enable some use cases that aren't relevant in PHP (yet) such as taking any generic type which can be converted into a T, because you only want to do that transformation conditionally on something else. -- PHP Internals - PHP Runtime Development Mailing List To unsubscribe, visit: https://www.php.net/unsub.php