Live data from Hacker News

Rust: “Explain GATs Like I'm 5 Years Old”

old.reddit.com

51–60 of 193 posts

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#51
post #35

I'm filing this one along with Monads and Haskell as programming concepts that I'll never understand. Rust is my favorite language, but that explanation, lauded directly below it as This is personally the easiest to understand example I've seen of GATs. , was incomprehensible.

You mention that you use Rust. In that case, you're almost certainly familiar with generics, and you're likely familiar with associated types. If you've ever found yourself struggling to write an associated type that needed a generic parameter, this feature is for you. And if you haven't ever needed to do such a thing, that's totally fine, and you can happily ignore it and content yourself with benefiting from librar…

I've yet to figure out why to use associated types instead of generic parameters, and every time I've seen an explanation I nod my head in confusion and carry on.

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#52

I'm filing this one along with Monads and Haskell as programming concepts that I'll never understand. Rust is my favorite language, but that explanation, lauded directly below it as This is personally the easiest to understand example I've seen of GATs. , was incomprehensible.

I think the easiest way to understand it is this: Suppose you have a concrete type like Vec Regular generics enable you to make the contained type generic, so you have: Vec where T can be i32, u32, String, etc. GATs allow you to make the container generic. So you can have: T where T might be Vec, Option, Box, etc.

Thanks, this was much easier to understand than the original article. What will I use this for? Usually for the “T” to be useful it needs to have some interface. In Java I’d just use Collection or something more abstract.

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#53

Is there any way to use GATs to achieve a particular use case of ah-hoc/anonymous enums[0]? Say if library L1 returns enum A|B, L2 operates on enum A|B, and main wants to pass enum A|B from L1 to L2, with the kicker being A, B are concrete in main. I'm not interested in distinguishing between multiple appearances of the same type as in the RFC example, so rather like: let foo: (~str|int) = (_|666); match foo { (s: st…

These are called sub-types or pattern types, and they are not in the language. But they are something I would love to have. You can’t emulate them with GAT unfortunately.

https://cohost.org/oli-obk/post/165584-ranged-integers-via

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#54

I'm filing this one along with Monads and Haskell as programming concepts that I'll never understand. Rust is my favorite language, but that explanation, lauded directly below it as This is personally the easiest to understand example I've seen of GATs. , was incomprehensible.

I think the easiest way to understand it is this: Suppose you have a concrete type like Vec Regular generics enable you to make the contained type generic, so you have: Vec where T can be i32, u32, String, etc. GATs allow you to make the container generic. So you can have: T where T might be Vec, Option, Box, etc.

this sounds almost too easy to understand, will have to check with the documentation. (thank you!)

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#55
post #49

Earlier quoted context omitted.

I think the easiest way to understand it is this: Suppose you have a concrete type like Vec Regular generics enable you to make the contained type generic, so you have: Vec where T can be i32, u32, String, etc. GATs allow you to make the container generic. So you can have: T where T might be Vec, Option, Box, etc.

THIS is literally the best explanation. My god people have forgotten how to explain anything anymore. Thank you.

It is easy to understand because it is an oversimplification :)

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#56
post #10

Rust better figure out quick how they can shut down the Haskell & C++ language artisans or it will be their downfall (in favor of something like Zig).

Rust getting more of Haskell's type system features is a good thing. The stronger your type system is, the more mistakes you can discover as compiler errors instead of runtime bugs.

There will always be more things that someone will want to formally verify that don't exist in the type system. How do you decide what is and isn't worth putting into the type system?

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#57

Earlier quoted context omitted.

Basically, before this you couldn't have generic types in traits, like this: trait X { type Y ; } You couldn't express "a thing that contains another thing" in one of those type expressions before, and also use the contained type. It's needed for Mappable, because it's an abstraction over the concept of modifying a value inside a container. IMO the implementation that they have is a bit obtuse, but I don't know if yo…

I really wonder about the usefulness of implementing a language feature to reduce the need to copy paste in an editor.

Generics are a language feature to reduce the need to copy and paste in an editor. For that matter, functions are a language feature to reduce the need to copy and paste in an editor.

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#58

I feel like they are making languages too complicated. Most projects should just be garbage collected, most don't need to deal with pointers or anything advanced. We need variables, functions, conditionals, loops, exception handling, struts, and a library with a bunch of standard utilities (including simple I/O and string managment). Everything else should be optional and kept out of sight unless you are looking for…

`&'static str` is an `&str` (string slice) with a `'static` lifetime (ie. lasting the duration of the program). It is most commonly encountered with string literals.

In some languages, it is appropriate to gloss over the differences between `String`, `&str`, and `&'static str`, but in Rust, all of those details are meaningful and important. If distinguishing between them isn't important to you, there are other languages that probably better reflect your needs.

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#59

Earlier quoted context omitted.

I think the easiest way to understand it is this: Suppose you have a concrete type like Vec Regular generics enable you to make the contained type generic, so you have: Vec where T can be i32, u32, String, etc. GATs allow you to make the container generic. So you can have: T where T might be Vec, Option, Box, etc.

What you're describing sounds to me like higher kinded types, which are of some relationship to GATs (GATs enable a better encoding of higher kinded types as I understand it), but are not GATs. https://docs.rs/higher/latest/higher/

> as I understand it

You could be 100% right (I have no idea, but it sounds reasonable), but this entire question really hits home that Rust has tons of basically incomprehensible design features.

The worst part about features like this are that a few smart people will actually use them (whether because they're powerful features, or just because they feel powerful when using them), and then render their code incomprehensible to the mere mortals who have to support their code in the future.

The KISS principle (https://en.wikipedia.org//wiki/KISS_principle) is sometimes ignored because people don't want to be stupid, and why wouldn't you want to use a new toy(tool) once you've figured it out?

Re: Rust: “Explain GATs Like I'm 5 Years Old”

#60
post #49

Earlier quoted context omitted.

THIS is literally the best explanation. My god people have forgotten how to explain anything anymore. Thank you.

It is easy to understand because it is an oversimplification :)

Which part is inaccurate/incomplete?
Post reply on HN