←back to thread

517 points bkolobara | 1 comments | | HN request time: 0s | source
Show context
veber-alex ◴[] No.45042068[source]
I find the Zig example to be shocking.

It's just so brittle. How can anyone think this is a good idea?

replies(5): >>45042182 #>>45042784 #>>45043157 #>>45045393 #>>45046983 #
kouteiheika ◴[] No.45043157[source]
Every language has questionable design decisions that lead to brittle code, although some more than others.

Like, how can anyone think that requiring the user to always remember to explicitly write `mutex.unlock()` or `defer mutex.unlock()` instead of just allowing optional explicit unlock and having it automatically unlock when it goes out of scope by default is a good idea? Both Go and Zig have this flaw. Or, how can anyone think that having a cast that can implicitly convert from any numeric type to any other in conjunction with pervasive type inference is a good idea, like Rust's terrible `as` operator? (I once spent a whole day debugging a bug due to this.)

replies(2): >>45043445 #>>45050774 #
veber-alex ◴[] No.45043445[source]
You are right, but it doesn't mean we can't complain about it :)

As a side note, I hate the `as` cast in Rust. It's so brittle and dangerous it doesn't even feel like a part of the language. It's like a JavaScript developer snuck in and added it without anyone noticing. I hope they get rid of it in an edition.

replies(3): >>45043464 #>>45043857 #>>45045791 #
JoshTriplett ◴[] No.45043464[source]
> As a side note, I hate the `as` cast in Rust. It's so brittle and dangerous it doesn't even feel like a part of the language. It's like a JavaScript developer snuck in and added it without anyone noticing. I hope they get rid of it in an edition.

Rust language hat on: I hope so too. We very much want to, once we've replaced its various use cases.

replies(2): >>45043819 #>>45044497 #
bigstrat2003 ◴[] No.45043819[source]
As a Rust user, I hope that "as" never goes away. It's way too useful to get rid of, e.g. deliberately truncating numbers to a smaller number of bits. Can you do that in other ways? Absolutely. But they are all less ergonomic than "as". At most I would be ok with restricting "as" to an unsafe block, though that isn't a perfect solution because unsafe isn't really meant to apply to such cases.
replies(1): >>45044125 #
JoshTriplett ◴[] No.45044125[source]
I'm hoping that we provide ergonomic alternatives for all the individual use cases. The brevity of `as` is a major reason we haven't removed it yet.

We have `.into()` for lossless conversions like u32 to u64.

We need to fix the fact that `usize` doesn't participate in lossless conversions (e.g. even on 64-bit you can't convert `usize` to `u64` via `.into()`).

We need to fix the fact that you can't write `.into::<u64>()` to disambiguate types.

And I'm hoping we add `.trunc()` for lossy conversion.

And eventually, after we provide alternatives for all of those and some others, we've talked about changing `as` to be shorthand for `.into()`.

replies(4): >>45045654 #>>45045700 #>>45047402 #>>45048310 #
1. muvlon ◴[] No.45047402[source]
> We need to fix the fact that you can't write `.into::<u64>()` to disambiguate types.

Yes, that would be great. In the meantime, if you can't wait but want something like this, you can DIY it via an extension trait.

It's very easy to write it yourself, this is all it takes:

  pub trait To {
      fn to<T>(self) -> T where Self: Into<T> {
          <Self as Into<T>>::into(self)
      }
  }
Now whenever this trait is in scope, you get to simply do .to::<u64>() and it does exactly what Into does. If you prefer adding a tiny dependency over copy-pasting code, I've also published a crate that provides this: https://crates.io/crates/to_method