Open Neme12 opened 1 year ago
Do you envision the Rune
to char
operator throwing if the Unicode scalar is out of range for char
?
@alexrp Yes, of course. That's how explicit casting usually works - if the value can't be converted to that type, an InvalidCastException
is thrown. Just like the existing cast from char
to Rune
throws if the char
is a surrogate or invalid Unicode scalar.
Sounds fine. I can see the usefulness of the Rune
to char
operator with those semantics.
I'm personally less convinced about the int
/uint
operators. But I also happen to think that the int
/uint
to Rune
operators probably shouldn't have been added in the first place, and instead only the Rune(int)
constructor should have existed... so maybe that's just my bias.
I don't really need those, but I added them to the proposal for symmetry since the opposite conversions exist as well. I think conversions should generally be symmetrical whenever possible - if you can convert A to B, you should be able to convert B back to A. (This is also probably why I tried casting to char
and expected that to Just Work™, because I knew that the opposite conversion exists.)
I don't feel like the int
/uint
conversion operators are a good idea, nothing about an int
/uint
explicitly says "this is a character/codepoint" unlike char
. Doing that conversion via rune.Value
is unambiguous and is something you can already do today.
I don't really need those, but I added them to the proposal for symmetry since the opposite conversions exist as well. I think conversions should generally be symmetrical whenever possible - if you can convert A to B, you should be able to convert B back to A. (This is also probably why I tried casting to char and expected that to Just Work™, because I knew that the opposite conversion exists.)
(That said, I don't feel strongly about that. I'm mainly interested in the char
one).
Tagging subscribers to this area: @dotnet/area-system-runtime See info in area-owners.md if you want to be subscribed.
Author: | Neme12 |
---|---|
Assignees: | - |
Labels: | `api-suggestion`, `area-System.Runtime`, `untriaged` |
Milestone: | - |
Background and motivation
The other day, I needed to convert a
Rune
tochar
when it fits within a singlechar
. I tried using an explicit conversion, expecting that to work, but to my surprise, such a conversion doesn't exist. I later found out that this can be achieved by(char)rune.Value
, but this isn't obvious, and I think the explicit conversion should exist given that a conversion fromchar
toRune
exists as well. If it works in one direction, I think it should work in the other direction as well, so this is what I'm proposing.API Proposal
Given that conversions from
int
anduint
exist as well, I added conversions back to those to the proposal as well for symmetry. These ones could actually be implicit because the conversion will always succeed, but I opted to make them explicit as I think making them implicit would be undesirable and could lead to bugs, just like the existing implicit conversion fromchar
toint
often causes bugs, so it's good to make the developer be explicit.API Usage
Alternative Designs
No response
Risks
No response