by wavemode · 1y ago · 0 comments
If all I need is a slice of memory, I'd rather only have to pass around the two pieces of data (pointer and length) than all three.
The growable array type (what you call "vector") is a venerable data type, although it does still have parameters you might reasonable tweak e.g. what's the correct growth factor? Doubling is popular but e.g. Zig chooses new_size = (old_size * 1.5) + 8 and you'll see disagreement about what API should be offered and why - e.g. C++ std::vector has the wrong reservation API
But this thing clearly isn't a mistake, it's intentionally a hybrid, and if it had been a huge success maybe everybody else would scramble to copy it.
A mutable String or Vec has all three fields, but the borrowed slice or str has just pointer and length.
Go chooses to be simpler by not having those kinds of separations, which also makes it a little less efficient under some circumstances.
This type isn't either of those things, it's a hybrid, and Go tries to get away with that because it's a garbage collected language so people don't need to care as much about ownership.