dart-lang / dart-lang/language

Allow `codeUnitAt` to be constant for constant strings

Open
#4,146 14 comments 3 reactions 0 assignees View on GitHub
feature
Dominant language
TeX
Stars
2.9k
Forks
239
Avg merge
2d 18h
Merged PRs (30d)
14

Description

Let's take the example from https://dart.dev/language/branches#switch-expressions:

```dart
// Where slash, star, comma, semicolon, etc., are constant variables...
token = switch (charCode) {
slash || star || plus || minus => operator(charCode),
comma || semicolon => punctuation(charCode),
>= digit0 && <= digit9 => number(),
_ => throw FormatException('Invalid')
};
```

It assumes that `slash`, `star`, ... are constant variables, but how are they defined? I expect to be able to do:

```dart
const slash = '/'.codeUnitAt(0); // Error: Methods can't be invoked in constant expressions.
// ...
```

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.

Contributor guide

Open the contributing guide

Research direction

Start with issue #4146 and the linked Dart switch-expression example, then read the language specification's rules for constant expressions and method invocation. Determine the language-design changes needed for `codeUnitAt` on constant strings and document the resulting decision and scope.

Written by the indexing model from the issue text.

Assessment

Tech stack
dart
Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.