Skip to main content

STRING

Constant STRING 

Source
pub const STRING: BuiltinSchema;
Expand description

String: an immutable sequence of characters, whose length counts characters rather than bytes — and every other index this type takes or answers, in chars, slice, and indexOf, counts the same way, so an API that mixed characters and bytes never has the chance to become a trap.

Three operations count in bytes instead, and every one of them says so in its name: byteLength, codePointAtByte and sliceBytes. They exist because the representation is UTF-8 and a scanner that walks it should not have to allocate a one-character String per character to do so (issue #292). The rule that keeps them from becoming the trap the paragraph above avoids is the naming one: a byte offset is not a String index, it is a value one of these three answered and another takes back, and no method without Byte in its name accepts one. Mixing the two index spaces in the same call is therefore something a reader can see rather than something the types allow silently.

join lives here rather than on Array<String>, so that ", ".join(names) reads receiver-first with the separator: BuiltinType has no way to constrain a receiver’s type parameter, so an Array<T>.join would either have to accept an Array<Int> too or need a bound the MVP cannot express, and putting the method on String instead sidesteps the bound rather than needing it.

split and replace both search for a piece of text that may not be empty: an empty needle would match between every character rather than answer either method’s question, so both refuse it at run time and point at chars(), which is the operation that actually means that.