4 ms·
Type-checked keypaths in Rust
- SnowflakeOnIce 4y agoI had not heard of keypaths before. They seem to provide functionality similar to lenses in Haskell.
- Gadiguibou 4y agoThat's really cool I hadn't heard of them before! For those that are interested, I found this article by FPComplete on the topic: https://www.fpcomplete.com/haskell/tutorial/lens/ https://www.fpcomplete.com/haskell/tutorial/lens/
- threeseed 4y agoAt least this implementation derives from NeXT's Enterprise Object Framework [1] created in 1994. Which then made its way into Apple WebObjects, Cocoa and finally Swift. [1] https://en.wikipedia.org/wiki/Enterprise_Objects_Framework https://en.wikipedia.org/wiki/Enterprise_Objects_Framework
- ekiauhce 4y agoGood article, thanks! There is similar concept for json https://goessner.net/articles/JsonPath/ https://goessner.net/articles/JsonPath/ We heavily using it for validation error messages. Like { "error": "wrong value", "path": "foo/bar[1]/prop" }
- threeseed 4y agoFor JVM languages you have OGNL: https://commons.apache.org/proper/commons-ognl https://commons.apache.org/proper/commons-ognl
- sideeffffect 4y agoJMESPath is what JSONPath should have been https://jmespath.org/ https://jmespath.org/ specified, standardized, with tests and implementations for multiple languages
- terhechte 4y agoTyped Keypaths are one of the Swift language features which I really miss in any other language. They're incredibly powerful because it is possible to compose them. Imagine a struct or class `Item` with a `user` field and the `User` has an `age` of `Int`: // Keypath<Item, User> (from Item to User) let item_user = \Item.user // Keypath<User, Int> (from User to Int) let user_age = \User.age // Keypath<Item, Int> (from Item to Int) let item_age = item_user.appending(user_age)
- hither_shores 4y agoMost languages with good support for functional programming have lenses, which generalize keypaths. Off the top of my head: Haskell, Purescript, TypeScript, Scala, OCaml, Racket, and Common Lisp all have lens libraries.
- wyager 4y agoFor (typed) optics to work well you need HKTs and universal quantification over those types, which rules out many of the languages you listed. Haskell and PureScript should be fine. I don't see how it would work in TS, scala, ocaml. Lisp should work, but no types right? No idea about racket.
- hither_shores 4y agoFor general optics, yes. But lenses and to a lesser extent prisms still work ok in their naive getter/setter and constructor/pattern encodings.
- shirogane86x 4y agoscala has HKTs and should be able to implement lenses just fine, I think. TS and OCaml can emulate HKTs in a really awkward way, but they can also just implement a less general more restricted subset.
- jasonhansel 4y agoIMHO the issue is that lenses are too powerful. For this reason, they are liable to be used in overly clever ways that reduce readability and increase complexity. Things like keypaths are useful because they are (intentionally) more restricted than lenses, and are thus less likely to be misused.
- sfvisser 4y agoAre keypath like Haskell lenses? First class composable getters/setters? Or just the getter part?
- lnyng 4y agoI created something similar for our project “below” (https://github.com/facebookincubator/below/blob/main/below/below_derive/src/lib.rs https://github.com/facebookincubator/below/blob/main/below/b...). The program collects system resource metrics into a data structure and we need to display the fields with different styles and formats. In order to decouple the data structure from rendering, Queriable (Keyable) and FieldId (combine KeyPath + mirror struct into enum) are used. I will definitely like to checkout the KeyPath implementation as it seems more general.
- pornel 4y agoI think it may be possible to make it even more of a compile-time construct if uglier types were an option. Instead of holding a `Vec<PathComponent>` it could have been a list made out of nested types: `Path<First, Path<Second, Path<Third, Last>>>>`.
- mkeedlinger 4y agoThis is quite cool, I'm just having a hard time imagining when I would use it.
- cmyr 4y agoI hope I cover this somewhat in the 'motivations' section. Keypaths are used heavily in SwiftUI, and my interest in them came from working on trying to find ergonomic patterns for GUI libraries in Rust.