Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Rust does not have HKT, and so we cannot write a generic Monad. Rust has many instances of Monad, though.

That said, at some point, we'll be getting ATC, which is equivalent to HKT in many cases, but feels more Rusty.



Thanks :) . Nit: less TLAs, please.

- HKT: Higher-Kinded Types

- ATC: Alternative Type Constructors


Yeah, sorry. It's "Associated" not "Alternative" by the way. A good reason to spell them out ;)


How does your implementation of associated types differ from the same feature in Haskell? In particular, why is this related to higher-kinded types?

From what I remember from the theory, higher-kinded types lead to a genuinely more difficult type inference problem (going from easy first-order unification to undecidable higher-order unification), while associated types are simply existential types (and in particular no more difficult than higher-rank polymorphism).


https://github.com/rust-lang/rfcs/blob/master/text/1598-gene... has all the gory details, reading that is probably best!


Ok, I did, so here's the answer in case anybody else is confused:

The feature under discussion is "associated type constructors". Rust already has associated types in traits (I didn't know that part and was confused), and what this feature adds is that it allows us to associate a first-order type constructor to a trait.

Since the type constructor is first-order, and first-order type constructors are already present in the base language in the form of generic types, the implementation is simplified to the point that it can reuse the existing infrarstructure for type inference.

---

Apart from that, the reason for this implementation choice seems to be that it's required for precise lifetime management. Almost all datastructures in rust seem to be parameterized with a lifetime argument, even if they have no further type parameters. Since there is no such thing as a second-order lifetime (i.e., a "lifetime constructor" T : (lifetime -> lifetime) -> lifetime), first-order type constructors are enough to handle all issues that pop up because of lifetime management.

---

That actually seems like a very pragmatic design. The only problem I had while reading this RFC is that the combination of "multiple-inheritance" in traits with their built in namespacing leads to some really ugly syntax, e.g., "<T as Foo>::Bar<'a, 'static>;". Is this already idiomatic rust?

There's several more things that confuse me in this RFC, but this is probably the wrong place to discuss these things. Incidentally, what is the right place to talk about this?


> Is this already idiomatic rust?

It's only used for disambiguation cases, extremely rarely. I've been doing Rust almost five years and I think I may have written <T as Foo> once.

> what is the right place to talk about this?

https://internals.rust-lang.org/ is the best place. Probably going to be pretty slow-going until next week, due to the holiday today.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: