c3c icon indicating copy to clipboard operation
c3c copied to clipboard

Standard library String

Open lerno opened this issue 3 years ago • 0 comments

There are 4 possible designs:

  1. String is stringbuilder type, non-opaque, char subarrays used where needed. ABI stability concerns.
  2. Same as (1) but String is opaque (i.e. just a pointer to it), which allows less static creation of it (but might not be needed). Better ABI stability.
  3. String is a distinct char subarray type with additional types. Problem is that a substring may revert to being a basic char subarray. So given String a = "123" then a[1..2] may yield a char[] substring rather than String substring. Added language features may fix that.
  4. As (2) but with capacity prepended the pointer. Basically one less indirection, but value is not stable under mutation.
// 1
String foo;
foo.append("hello");
foo.delete()
// 2
String foo = string::new();
foo.append("hello");
foo.delete();
// 3
String foo = "hello";
String bar = foo[1..2]; // <- needs compiler support
// 4
String foo = string::new();
String bar = foo; // Bad
foo.append("hello");
// bar may be dangling now!
foo.destroy();
// bar is most certainly dangling here

lerno avatar Jun 05 '22 15:06 lerno