News for hackers. Stuff that matters.
Re:Rust: “Explain GATs Like I'm 5 Years Old” by kibwen on Monday November 07, 2022 @08:58AM (#33505816)
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 libraries that are now able to expose simpler and more powerful APIs by leveraging this feature under the hood.
Re:Rust: “Explain GATs Like I'm 5 Years Old” by KallDrexx on Monday November 07, 2022 @09:22AM (#33506092)
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” by Nullabillity on Monday November 07, 2022 @09:52AM (#33506510)
You can view generic parameters as "inputs", they are controlled by the consumer of the type/trait (you construct a `Vec<String>` because you want to store a bunch of `String` values).

Associated types are "outputs", they are controlled by the impl (`<Vec<String> as IntoIterator>::into_iter()` will always return a `std::vec::IntoIter<String>`, because that is how the trait is implemented for `Vec<T>`).

You can often use generic parameters for "outputs" as well (you could have a world where you had `impl<T> IntoIterator<IntoIter<T>> for Vec<T>`). However, this prevents the compiler from inferring the "output" type since there is no longer a guarantee that it is unique for a given set of "inputs".

Re:Rust: “Explain GATs Like I'm 5 Years Old” by gpderetta on Monday November 07, 2022 @10:24AM (#33506963)
You can generalize this:

A: type level functions from types to (associated) types (i.e. normal generics)

B: type level functions from types to type level functions (either As or Bs) (GAT).

with generic parameters (i.e. higher kinded types, or template template parameters in C++) you could have type level functions that map type functions to (types or type functions), but once you have GATs you can fake them by passing to first order functions a type that has an associated type.

Re:Rust: “Explain GATs Like I'm 5 Years Old” by kibwen on Monday November 07, 2022 @09:57AM (#33506569)
Associated types serve much the same role as generic parameters in the context of traits, but with the crucial distinction that associated types are specified by the person implementing the trait for their type, whereas generic parameters are specified by the person calling the methods defined by the trait.

In practice you could basically just have generic parameters and not bother with associated types, but it would be much less nice to use these APIs.

Here's an example of how an API would change if Rust didn't have associated types:

    let x = [1,2,3]; // an array of i32
    let mut y = x.iter(); // an iterator over it
    y.next(); // pull an item from the iterator
If Rust didn't have associated types, then that last line would have to look like this:

    y.next::<&i32>(); // if Rust didn't have associated types
Re:Rust: “Explain GATs Like I'm 5 Years Old” by jamincan on Monday November 07, 2022 @10:00AM (#33506622)
The Rust book has a pretty good explanation of the difference. https://doc.rust-lang.org/book/ch19-03-advanced-traits.html
Re:Rust: “Explain GATs Like I'm 5 Years Old” by the__alchemist on Monday November 07, 2022 @09:02AM (#33505872)
I appreciate the explanation. I've been more of a firmware or application developer in Rust than library developer, so will keep this in the hip pocket.
Re:Rust: “Explain GATs Like I'm 5 Years Old” by jackmott42 on Monday November 07, 2022 @09:11AM (#33505957)
Yeah basically if the day comes when you need these, you will just naturally use them and it will work and you will never think anything of it.

Whereas I stumbled upon a use for these ~4 years ago and was just mad that what I naturally wanted to do didn't work =)

But yeah in certain programming domains, probably most, it just doesn't come up.

Re:Rust: “Explain GATs Like I'm 5 Years Old” by mcronce on Monday November 07, 2022 @09:21AM (#33506072)
Yeah - it's almost better to think of GATs as less of a feature and more of a weird restriction being removed. That's obviously super reductive knowing the amount of work that went into it, but from an end developer standpoint, it's an explanation that helped make it make sense to me.