-
Notifications
You must be signed in to change notification settings - Fork 205
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
Allow codeUnitAt
to be constant for constant strings
#4146
Comments
What would be the difference between const slash = '/'.codeUnitAt(0); and what we have now? const slash = '/'; It seems you want a char type of some type? I'm not sure if it's even possible, as JS doesn't support it. |
You can parse things like
No need for an extra type; const digit0 = '0'.codeUnitAt(0); Instead of const digit0 = 48;
assert(digit0 == '0'.codeUnitAt(0)); |
I would rather have a char type, it could even be backed by int, like an extension type on int, but with a literal constructor. I don't think it's so feasible for Dart today, but there's an issue for this: #886. |
I made a package with character code points, Making |
Feel free to close this then.
Thanks, I didn't know about this package.
It won't matter for ASCII characters only, which is my usecase. But yeah making |
It's not that I haven't wanted There is no end to the extension to the constant sub-language that it's possible to add, but the language team is generally choosing not to add small constant features. Every little thing is a little thing, but the cummulative complexity adds up. Anything that can be constant needs to be specified more precisely, including when it fails (because that can cause compile-time errors). The analyzer needs to be able to evaluate it, because it evaluates constants, but it doesn't otherwise compile or run code. Allowing something to be constant might make some later changes harder, if those are not compatible with being constant. (I don't see what that could possibly be for |
Why not write an final class int {
external const factory int.charCodeAt(String string, [int index = 0]);
}
const int b = int.charCodeAt('b'); |
|
A magical We generally try not to add that kind of constructors, which are not really constant, they just pretend to and require the compliler to do computation at compile-time. (We used to have a It's definitely an easier "language feature" than a code unit/code point syntax like I do agree that the connection between |
Let's take the example from https://dart.dev/language/branches#switch-expressions:
It assumes that
slash
,star
, ... are constant variables, but how are they defined? I expect to be able to do:but I can't, and it's ugly and error-prone to find out what the char code for each of the characters are and put the integer in instead.
The text was updated successfully, but these errors were encountered: