Re: Type casting syntax
| From: | Levi Morrison via internals | Date: | Sun, 11 Jul 2021 22:30:57 +0000 |
| Subject: | Re: Type casting syntax | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-115390@lists.php.net to get a copy of this message | ||
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.