Skip to content

String.fromCharCode and String.fromCharCodes should be const constructors #49407

Open
@rakudrama

Description

@rakudrama

I have needed to construct constant expression strings from their code points.
This usually happens in programs that do some string processing with code points.
Sometimes it is desired to define named constants for both the code unit and the String.

This is currently impossible to construct a constant String form a code point, and impossible construct a const code point from a String. This leads to constants that are not obviously consistent:

const codeA = 0x41;
const charA = 'A`;

These could be correct by construction:

const codeA = 0x41;
const charA = String.fromCharCode(codeA);

dart-lang/language#2219 proposes making aString.codeUnitAt(anInt) a potentially constant expression, permitting

const codeA = charA.codeUnitAt(0);
const charA = 'A';

However, using String.codeUnitAt has a UTF-16 pitfall. The following is correct

const codePerson = 0x1F9D1;
const charPerson = String.fromCharCode(codePerson);

whereas this not:

const codePerson = charPerson.codeUnitAt(0);
const charPerson = '🧑';

This request is broken out from dart-lang/language#2219 (comment)

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-core-librarySDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.library-coretype-enhancementA request for a change that isn't a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions