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

Well, Haskell does have garbage collection, but why would lack of garbage collection make this problematic? (There seems to be a Rust crate for lenses: https://lib.rs/crates/lenses, though I can’t assess it as I’ve never used Rust.)


That's interesting. That crate provides immutable access only and requires the fields to be trivial to copy. For example, I think there's no way for you to mutate one field if you've handed out a lense to a different field.

The core problem would be you can't write something like this

  fn return_part() -> &Part {
    let whole = get_whole();
    &whole.part
  }
because whole is "dropped" at the end of the function call.

If you want to hand out parts you have to put the whole somewhere where it will stick around until you're done with the parts, and doing that nicely pretty much requires GC (you could also use lenses only in very restricted scopes or just leak memory).




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

Search: