<story><title>Go(lang): Robust generic functions on slices</title><url>https://go.dev/blog/generic-slice-functions</url></story><parent_chain></parent_chain><comment><author>stouset</author><text>I feel like I’m taking crazy pills. How are these APIs even remotely defensible?&lt;p&gt;&lt;pre&gt;&lt;code&gt;    slices.Sort(s)          &amp;#x2F;&amp;#x2F; fine
    slices.Compact(s)       &amp;#x2F;&amp;#x2F; broken
    s  = slices.Compact(s)  &amp;#x2F;&amp;#x2F; fine
    s := slices.Compact(s)  &amp;#x2F;&amp;#x2F; broken (!!!)
    slices.Delete(s, …)     &amp;#x2F;&amp;#x2F; broken
    s = slices.Delete(s, …) &amp;#x2F;&amp;#x2F; fine
&lt;/code&gt;&lt;/pre&gt;
How is one intended to remember which functions require overwriting (due to invalidating) their input and which don’t? Why does the language make it so easy to render function parameters unusable upon return but impossible to enforce that they aren’t used afterward?&lt;p&gt;How on earth did it take twelve years for this “simple” language to make a function to delete elements from an array, with `s = append(s[:start], s[end:]...)` having to suffice until then? How on earth does the “better” API twelve years later have such a gaping footgun? This is a &lt;i&gt;core&lt;/i&gt; type that quite literally every program uses. How have things gone so far off the rails that “setting the obsolete pointers to nil” is such an intractable problem for end users they had to add a new keyword to the language?&lt;p&gt;For other languages I see posts where weird language corner cases bring up challenging issues that really reinforce the idea that language design is hard. Rust—for example—has unsoundness issues in corner cases of the type system. But for go, it feels like there’s a constant stream of own goals on core areas of the language where the design should have knocked it out of the park. “Simple manipulation of arrays” just should not have this many footguns.</text></comment>
<story><title>Go(lang): Robust generic functions on slices</title><url>https://go.dev/blog/generic-slice-functions</url></story><parent_chain></parent_chain><comment><author>TheDong</author><text>The problems with the API they point out are almost all things that rust&amp;#x27;s ownership system was built to solve.&lt;p&gt;Things like:&lt;p&gt;&lt;pre&gt;&lt;code&gt;    slices.Sort(s) &amp;#x2F;&amp;#x2F; correct
    slices.Compact(s) &amp;#x2F;&amp;#x2F; incorrect
    slices.Delete(s, ...) &amp;#x2F;&amp;#x2F; incorrect
    s := slices.Delete(s, ...) &amp;#x2F;&amp;#x2F; incorrect if &amp;#x27;s&amp;#x27; is referenced again in the outer scope
    s = slices.Delete(s, ...) &amp;#x2F;&amp;#x2F; correct
&lt;/code&gt;&lt;/pre&gt;
All of those are solved by having functions like &amp;#x27;slices.Sort&amp;#x27; take a &amp;#x27;&amp;amp;mut&amp;#x27; reference in rust speak, and having &amp;#x27;slices.Compact&amp;#x27; and &amp;#x27;Delete&amp;#x27; take an owned slice, and return a new owned slice.</text></comment>