Hacker News new | past | comments | ask | show | jobs | submit login

Does anybody know if GATs would enable to issue indexes/handles tied to a container without the use of closures? This would allow compile-time checks that indexes are only used with their container.

The most advanced crate for such use-case is `indexing` [0] but it is pretty complex inside, requires closures and seems more like an experiment.

[0]: https://github.com/bluss/indexing




In principle, the ingredient you need for that use case is "existential types". I suspect GATs let you define existential types -- after all, the selling point is that the trait defines some type, and you can't rely on what type any particular implementor chooses. (But I can't quite put my finger on why non-G associated types shouldn't be enough.)

In Java(!), I have a class `Database<$Database>` whose constructor is private and whose sole static factory returns `Database<?>`. Thus, the caller must assume that every instance they create is parameterized over a distinct unknown type -- even if, in the implementation, $Database = Object. The dollar-sign prefix is a hint to the reader that this type parameter is meant to be a type correlated with a unique value.

You can get an `Index<$Database>` from any database, and it's parametrized over the owning database's unique existential type. Indexes over two different databases can't be confused.

The only issue (so far) is that you have to be careful not to "forget" the correlation between the type parameters on an index and its owning database. If the type parameter decays back into a wildcard, the correlation is lost.

[1] https://varkor.github.io/blog/2018/07/03/existential-types-i...




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

Search: