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

Reply via email to