Skip to content

Allow codeUnitAt to be constant for constant strings #4146

Open
@HosseinYousefi

Description

@HosseinYousefi

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

// 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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    featureProposed language feature that solves one or more problems

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions