4 ms·
For those that want to see what it actually looks like: https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0282-record-dot-syntax.rst#example
by hardwaresofton 6y ago
For those that want to see what it actually looks like:
https://github.com/ghc-proposals/ghc-proposals/blob/master/proposals/0282-record-dot-syntax.rst#examples https://github.com/ghc-proposals/ghc-proposals/blob/master/p...
data Grade = A | B | C | D | E | F
data Quarter = Fall | Winter | Spring
data Status = Passed | Failed | Incomplete | Withdrawn
data Taken =
Taken { year :: Int
, term :: Quarter
}
data Class =
Class { hours :: Int
, units :: Int
, grade :: Grade
, result :: Status
, taken :: Taken
}
getResult :: Class -> Status
getResult c = c.result -- get
setResult :: Class -> Status -> Class
setResult c r = c{result = r} -- update
setYearTaken :: Class -> Int -> Class
setYearTaken c y = c{taken.year = y} -- nested update
getResults :: [Class] -> [Status]
getResults = map (.result) -- selector
getTerms :: [Class] -> [Quarter]
getTerms = map (.taken.term) -- nested selector
- misja111 6y agoI love this syntax, it's elegant and a big improvement! When/ how can I use this? Do I have to wait for a new GHC release?
- hardwaresofton 6y agoIt's not released yet AFAIK -- there's a tracking issue on the GHC tracker[0]. I agree that it's very nice, and a big improvement! [0]: https://gitlab.haskell.org/ghc/ghc/-/issues/18599 https://gitlab.haskell.org/ghc/ghc/-/issues/18599
- Lambdanaut 6y agoUgh fucking finally. Too many hours wasted learning the complexities and template macro nonsense of lenses. I am so elated to see this change, even if it is 15 years overdo. Nested selectors in particular are beautiful. Maybe it's time again to do me some haskell for great good.
- prof-dr-ir 6y agoCould you explain why you think this proposal would help you avoid lens? If I am not mistaken all of the above code can easily be rewritten in the old syntax without using lens, so you probably have another use case in mind?
- deleted 6y ago[deleted]
- johnday 6y agoIt is very cumbersome to write nested updates using the old syntax, and overlapping names between records was quite painful to deal with.
- runeks 6y agoYou should have included a record field of the same name in both data types, since this is where the proposal really shines. E.g. if both data types were to contain an "id" field then "taken.id" and "class.id" wouldn't result in a compilation error as it does now.
- hardwaresofton 6y agoOh that's another big issue, thanks for pointing that out! Accessor functoin collisions was already somewhat solved, but this is a much nicer solution. That code is copy-pasted by the way, from the proposal.