3 ms·
I think golang's slices are a better solution than the linked article. https://go.dev/ref/spec#Slice_types https://go.dev/ref/spec#Slice_types Slices can stil
by mjevans 4y ago
I think golang's slices are a better solution than the linked article.
https://go.dev/ref/spec#Slice_types https://go.dev/ref/spec#Slice_types
Slices can still be nil (null), but it isn't an unsafe memory access operation, just another type of potentially useful or potentially errant invocation to handle.
- josephg 4y agoZig and Rust also support array slices. But they can’t be null, because that’s - as the article says - a mistake. https://ziglang.org/documentation/master/#Slices https://ziglang.org/documentation/master/#Slices https://doc.rust-lang.org/book/ch04-03-slices.html https://doc.rust-lang.org/book/ch04-03-slices.html
- WalterBright 4y agoD slices can be null, but they're not a mistake, as the runtime will not let you read/write a 0 length array.
- saagarjha 4y agoWhat's the relation between a null slice and a 0-length array?
- WalterBright 4y agoNeither can be successfully indexed.
- feffe 4y agoPerhaps this is similar to how they work in go? "var x []int; fmt.Printf(`%p %d`, x, len(x))" outputs "0x0 0" Indexing "x[0]" results in: "panic: runtime error: index out of range [0] with length 0" They can also be appended to and then produce a valid slice.
- pjmlp 4y agoOther languages avoid the mistake by preventing direct access unless either there is a null check or they are declared as non nullable.
- kristoff_it 4y agoGo's slices are more akin to ArrayLists / Vectors in other languages, since they also manage the underlying buffer.
- deleted 4y ago[deleted]