zlacker

[parent] [thread] 15 comments
1. lifthr+(OP)[view] [source] 2024-02-24 06:19:51
If immutable and mutable slices are differently typed [2], it is natural to define two functions (say, `slices.Compact` vs. `slices.Compacted`) to handle each type, like Python `list.sort` vs. `sorted`. It should be natural to expect `slices.Compacted` to never alter its input, and any attempt to use a mutable version will be very explicit except for slicing [1].

[1] Especially given that the capacity is preserved by default, which contributes to the current confusion. See my older comment: >>39112735

[2] Originally "...are different" but edited for clarity.

replies(3): >>ikiris+K >>makapu+R1 >>tsimio+Ey
2. ikiris+K[view] [source] 2024-02-24 06:29:47
>>lifthr+(OP)
This sounds awful in practice having to memorize different functions for the same thing based on mutability of the thing.
replies(2): >>ttymck+v1 >>shakow+Og
◧◩
3. ttymck+v1[view] [source] [discussion] 2024-02-24 06:41:35
>>ikiris+K
In practice it's not that bad
4. makapu+R1[view] [source] 2024-02-24 06:46:57
>>lifthr+(OP)
This would not allow the previous errors to be checked by the compiler since the main thing you're relying on is the name. Nothing prevents you to call deleted(mutable) and discard the result apart from the name.
replies(3): >>comex+Z3 >>Releas+P4 >>lifthr+q5
◧◩
5. comex+Z3[view] [source] [discussion] 2024-02-24 07:20:16
>>makapu+R1
Indeed. Though, Rust does have a way to mark a function such that any caller that implicitly discards its return value gets a compiler warning. This feature largely solves the problem you’re talking about. But it’s orthogonal to Rust’s borrowing system or mutable versus immutable distinction.

That said, I’d also point out that, while you can more or less replicate the Go example with Rust slices, in Rust it would be more idiomatic to pass around a Vec (or a mutable reference to a Vec) if a callee needs to do something like change the length. And you can’t resize a Vec if there are other references to its contents.

replies(1): >>Releas+a5
◧◩
6. Releas+P4[view] [source] [discussion] 2024-02-24 07:29:59
>>makapu+R1
> Nothing prevents you to call deleted(mutable) and discard the result

The Go compiler generates an error when you are (silently) ignoring the return value of any function. Or, to put it in other words, every compiler which does allow to (silently) ignore the return value of a function, should not be used at all (C++ has at least `[[nodiscard]]` since 17 and C with C23 - which is "too little and too late", as always).

replies(1): >>maskli+Ze
◧◩◪
7. Releas+a5[view] [source] [discussion] 2024-02-24 07:35:36
>>comex+Z3
`#[must_use]`. I really don't know why Rust made that error.
replies(1): >>lifthr+n6
◧◩
8. lifthr+q5[view] [source] [discussion] 2024-02-24 07:38:43
>>makapu+R1
While that's a valid concern, it is an orthogoal issue as it can be similarly replicated in Rust as well. Rust references always track mutability but we can sidestep that by using `std::borrow::Cow`:

    fn compacted<T: ...>(input: Cow<'_, [T]>) -> Cow<'_, [T]> { ... }
Then it is clear that, for example, `compacted(vec![...].into());` as a statement will exhibit the same behavior because `Cow` doesn't have `#[must_use]`. Rust avoids this issue mainly by encouraging explicitly mutable or immutable values by default, and at this point the language should have substantially altered that Go can do the same.
replies(1): >>nrabul+U6
◧◩◪◨
9. lifthr+n6[view] [source] [discussion] 2024-02-24 07:56:40
>>Releas+a5
I think it is notable that both `try!` (`?` today) and `#[must_use]` (originally restricted to `Result`, then made available in general later) appeared in the same release (0.10). In the other words, `#[must_use]` was strongly tied to `Result` back then. While we can put `#[must_use]` to any type now, the set of types that absolutely have to be `#[must_use]` remains relatively small, with a major addition being iterators and futures. Once they have been covered, any additional value from adding `#[must_use]` is not large enough to make it default, I think.
replies(1): >>kibwen+Gy
◧◩◪
10. nrabul+U6[view] [source] [discussion] 2024-02-24 08:04:34
>>lifthr+q5
must_use doesn’t have to be on a type, it can be applied to a function
◧◩◪
11. maskli+Ze[view] [source] [discussion] 2024-02-24 10:11:58
>>Releas+P4
> The Go compiler generates an error when you are (silently) ignoring the return value of any function.

It does not. You can actually infer that from TFA listing cases as problematic which would be caught by such a compilation error, and confirming it by just compiling them:

    a := []int{}
    // compiler says nothing
    slices.Delete(a, 0, 0)
The builtins are special cased to error if their return value is ignored (except for copy and recover).

> C++ has at least `[[nodiscard]]` since 17 and C with C23 - which is "too little and too late", as always

You can't even mark your own functions or types as nodiscard in Go. You need third-party tooling even just to ensure you're not unwittingly ignoring error results:

    f, err := os.Create("/tmp/filename")
    if err == nil {
        return
    }
    // compiler doesn't say anything even though f.Close returns error
    f.Close()
replies(1): >>Releas+rg
◧◩◪◨
12. Releas+rg[view] [source] [discussion] 2024-02-24 10:34:56
>>maskli+Ze
Sorry, yes, you are of course right. That's the linter which complains, not the compiler.

I have to say that I don't understand the rationale of a compiler that errors on unused variables but lets the user silently ignore function return values. As a solution to explicitly ignore return values already exists in the language.

◧◩
13. shakow+Og[view] [source] [discussion] 2024-02-24 10:40:43
>>ikiris+K
Programming is hard and one can not destroy complexity, only move it; other news at 12.
14. tsimio+Ey[view] [source] 2024-02-24 14:28:23
>>lifthr+(OP)
That would be nice, sure, but it still doesn't fix the problem for mutable slices. Immutable slices are a distraction - this discussion is explicitly about how the mutable version is supposed to work. Unless you want to argue that mutable arrays/slices shouldn't exist at all, of course.
◧◩◪◨⬒
15. kibwen+Gy[view] [source] [discussion] 2024-02-24 14:28:44
>>lifthr+n6
> While we can put `#[must_use]` to any type now, the set of types that absolutely have to be `#[must_use]` remains relatively small

Agreed, although conversely if we could start Rust development all over again I'd probably argue that must_use on functions (as opposed to on types) should probably be the default behavior. (These days it basically is the default behavior for functions in the standard library.) Though Rust didn't gain the ability to annotate functions with must_use until 1.27. Switching the default could perhaps be a good candidate for an Edition.

replies(1): >>tialar+sW
◧◩◪◨⬒⬓
16. tialar+sW[view] [source] [discussion] 2024-02-24 17:11:54
>>kibwen+Gy
It's too late to propose new features for Edition 2024, and you would first need to get some attribute agreed which has the opposite effect, then write up how your proposal works and see if it's generally agreed. I doubt that but you should certainly try.
[go to top]