←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 #
ChadNauseam ◴[] No.45045700[source]
> We need to fix the fact that you can't write `.into::<u64>()` to disambiguate types.

This confused me too at first. You have to do `u64::from(_)` right? It makes sense in a certain way, similar to how you have to do `Vec::<u64>::new()` rather than `Vec::new::<u64>()`, but it is definitely more annoying for `into`.

replies(1): >>45046726 #
JoshTriplett ◴[] No.45046726{3}[source]
Yeah, you either need `u64::from` or `let x: u64 = expr.into()` or similar.

It does "make sense" but it's obnoxious, and we should have something better.

replies(1): >>45048428 #
1. estebank ◴[] No.45048428{4}[source]
If only we had type ascription on expressions... ducks