Re:Rust: “Explain GATs Like I'm 5 Years Old”
by brap on Monday November 07, 2022 @09:24AM
(#33506108)
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<Integer> or something more abstract.
[ Parent
| on HN ]
Re:Rust: “Explain GATs Like I'm 5 Years Old”
by mrkeen on Monday November 07, 2022 @10:14AM
(#33506834)
T keeps its type info statically. So if you put a Set in, you can get a Set out.
Casting to Collection loses the static type info, so you couldn't write foo(bar(set)) if bar returned a Collection but foo accepted a Set.
Re:Rust: “Explain GATs Like I'm 5 Years Old”
by orblivion on Monday November 07, 2022 @10:32AM
(#33507080)
I forget what exactly a GADT is (even though I've used Haskell a bit before). But just going by the linked example from Reddit, you could make a generic `map` function that worked on any container type. You'd have to implement the map function separately for each type. But then you could make a bunch of utility functions that you'd implement once that use the generic `map` under the hood, and all of those container types would be able to use those utility functions.
Not a Rust guy but I think that's the idea.
Re:Rust: “Explain GATs Like I'm 5 Years Old”
by kibwen on Monday November 07, 2022 @10:45AM
(#33507292)
Just so that we're clear on acronyms for everyone out there, "GADT" is a computer science term for "generalized algebraic data type", which has no relation to "GAT", which is just Rust terminology for generics in associated types (even in Rust, I doubt that anyone will bother to refer to "GAT" in the coming years, rather than just lumping them in with the concept of associated types in general; there's really no need to teach them separately).
Re:Rust: “Explain GATs Like I'm 5 Years Old”
by ghostwriter on Monday November 07, 2022 @06:11PM
(#33513852)
> you could make a generic `map` function that worked on any container type.
not only container types, it can be any type that has a consistent context of evaluation for the final value being calculated: "doing async IO", "doing optional presence", "doing potentially erroneous extraction", and so on. In that sense, "doing multiplicity of values" is the context expressing a container type you mentioned as an example.