-
Notifications
You must be signed in to change notification settings - Fork 18k
proposal: make rune be a new type of concrete type int32, not an alias #29012
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
The recent blog entry https://blog.golang.org/go2-here-we-come suggests that each language change should address an important issue for many people with minimal impact on everybody else. To me this issue, while real, doesn't seem all that important, and making this change would break an unknown number of currently working and correct Go programs. At first glance I'm not sure this passes the bar. |
Exactly the same arguments could be applied to Personally, I've never found these aliases confusing. If I'm using characters (or whatever one should call them in Go) I use the aliases, otherwise I use the ordinary numeric types. Also there's no inheritance in Go and so Sorry, but I'm not a fan of the idea. |
breaking changes from the rune constant change are rare, and can be addressedAs it stands, my proposed changes to rune constants would break parsers that operate on unicode codepoints as numeric values where they are compared with non-rune types. The most common use of these constants is, to my knowledge to do c-style (?) codepoint parsing, like this: func allLatinAlpha(s string) bool {
for _, r := range s {
if !((r <= 'a' && r >= 'z') || (r <= 'a' && r >= 'z')) { return false }
}
} This would not be broken by my constant proposal (since all types are I think there are ways to potentially address this use case by making the semantic upgrade of // this is allowed because the untyped integer `3` can be converted to rune implicitly
'a' + 3
// this is not allowed because the untyped rune 'a' cannot be converted to int32 implicitly
'a' + int32(3) Though this is something the type system already uses with 'ideal' using the rune and int32 types like this is rareIt's very rare to have I'd love to have better data on this, if I could use, for example godoc.org as a dataset.
|
Except that
All this proposal does is force you to use separate rune/int32 types. You can still easily convert between the two if you need to.
Yes, that's what this proposal is trying to do. Represent them as separate types, because that's how they should be treated. Separate concepts should have separate types. If I make a UUID type, it should be a separate type from |
While I overall support this proposal, I don't like the idea of untyped rune constants being separate from untyped integer constants, a rune constant should still be an untyped integer constant, and all untyped integer constants should be assignable to runes. This remains consistent with how type definitions work in current Go. |
Well, remembering that strings are a sequence of bytes and that you can (and frequently do) assign rune literals in the ASCII range to byte variables, I don't agree that people think of bytes as being just numbers. If they did, then why bother having the byte alias in the first place? If we could go back in time to Go 1.0, then I would be much more sympathetic to this proposal as I remember being surprised when I first studied Go that it didn't have a separate 'char' type as many other languages do. However, making this change in Go 2 is going to break a fair bit of code and, for me at least, it certainly doesn't satisfy the recently introduced '10 x gain/pain' criterion for filtering out potentially implementable proposals. |
Several other languages are the exact same - C family has That being said, I personally don't think it should be a good practice since in my head I see them as different types. |
This ... is odd, and I don't understand why you would go out of your way to do this. None of the functions in
We're getting into the semantic weeds of why types exist here.
Why do I mention this? In more dynamic languages that Go draws inspiration from, types don't just confer size, they also confer meaning. Why do we use It's not just for ease of typing -- these types all confer contextual meaning. If I see a
|
I wrote a lot of words on my last comment about type systems, but I think a more succinct way to explain what I mean is that in a typical program you might define |
rune
a subtype of int32
, rather than an alias
We want to collect data on how much code this would break in the wild. @griesemer was going to work on that. |
I agree that This is also useful for marshaling/unmarshaling. Assume the following struct: struct Message {
Val1 rune
Val2 []rune
} Then the encoder may decide to encode that |
No change in consensus, so declined. |
This proposal has been added to the active column of the proposals project |
Shouldn't have declined, at least not yet. |
No change in consensus, so declined. |
This proposal has been added to the active column of the proposals project |
Given our emphasis on Go 1 compatibility, we have no way to make this change in a future version of Go. |
This proposal has been declined as infeasible. |
This proposal has been added to the active column of the proposals project |
Issue
Currently,
rune
s are aliased toint32
s. This has existed before type aliases existed and it works like this:This also means, rather more subtly that functions that handle
rune
s orint32
s implicitly support inputs of each other without any kind of warning or error, like this:Though it makes sense knowing of the little-known aliasing feature, this gets especially confusing in cases where
rune
s are rejected and it's not mentioned that they're an alias:Because
rune
andint32
are aliased to each other, it's not possible to put both in a type switch, returning a mysterious and confusing error like this:Of course, if you reflect a rune, it thinks it's an
int32
:It is additionally odd that rune constants are untyped numeric constants not actually of type
rune
and will do things ordinary runes can't if you try to perform an operation on them for example:Proposal
It's my opinion that it can be surprising and inconvenient to those new to the language that runes are invisibly
int32
s. It just makes sense that operations on runes should require a conversion, as such:Additionally, rune constants could be of type
untyped rune
, the same waystring
constants are typeduntyped string
:In a modern language that standardises on runes as UTF-32 encoded characters, I think it only makes sense for runes to be only explicitly convertible to types that osentiably represent numeric scalars.
It's my understanding that, since a this juncture it's impossible to know if a value is a
rune
orint32
,fmt
and similar tools can only print the numeric value of the character, rather than the code point it represents.The text was updated successfully, but these errors were encountered: