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

This is a very fair criticism, and I believe lenses are the solution to this problem, although I'm not advanced enough in Haskell to provide a round counterargument.


Lenses are amazing. However, they don't solve this issue. What they do is let you pack a getter and a setter in a single "data type" and do operation on them. Say you have a School record with a students field (a list of Student records). You can write a one-liner which, given a School, lets you access only those students with a name starting by 'a'. Or even better, lets you return a copy of School where only those aforementioned students get their grades doubled.

However, you still run into the same namespacing issue we talked about, because at the end of the day, lenses are a bit of wonderful magic on top of the existing, crummy record system.

You'd create a Lens like so (with Control.Lens):

  -- You're supposed to prefix by _ due to Template Haskell magic following, but it's still a plain record, namespacing issues and all
  data Foo = Foo { _bar :: String }
  -- TH magic making lenses for all fields
  makeLenses ''Foo
Now you have a 'bar' lens:

  -- prints 'bar'
  putStrLn (foo^.bar)
  -- creates a copy of 'foo' with 'bar' set to 'barbar'
  let foo' = foo & bar .~ "barbar"




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

Search: