logoalt Hacker News

jjmarryesterday at 8:33 PM1 replyview on HN

> Imagine if this was a new language that the dev community was seeing for the first time. It's hard to imagine it gaining much traction.

But it's not a new language. It's backwards compatible with C.

So "iterators" behave the same as pointers, since that's how you'd iterate through an array. You can add and subtract, then pass them to other functions.

You can't just have a function that returns a vector of strings, because that function would do an allocation. When is it deallocated? Before unique_ptr (the guide was written before), it'd be the caller's responsibility to manually do so.

Meaning you have to assign the output of that function to a variable every single time and manually remember to deallocated it or you get a memory leak.

C avoids this with `strtok` by destructively modifying the string in place. This is arguably worse.

If you were designing a new, non-GC, language, you'd have good ownership semantics and not allow pointer arithmetic. That'd be Rust.


Replies

WalterBrighttoday at 12:36 AM

D doesn't allow pointer arithmetic in @safe code. At first it seems like that cannot work, but it works very well. Pointer arithmetic is relegated to functions that are @system.

The reason it works is because D has actual array types.

If you choose to use automatic memory management with D, you are memory safe.