Description
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)