Advantages of SYB (scrap your boilerplate) over GHC Generics
Asked Answered
A

1

8

Are any tasks that are possible only with SYB, or are much easier with it, when compared to GHC Generics?

Adaiha answered 12/11, 2014 at 18:5 Comment(0)
C
3

GHC Generics is a rather verbose way to perform basically any query or traversal. For example, consider a language AST with Stmt and Expr types that both derive Typeable, Generic, and Data:

data Stmt = ... lots of constrs ...
data Expr = Const Int
          | ... lots of other constrs ...

How do you leverage SYB to get all constants starting from either Expr or Stmt? Something like:

getConst (Const i) = [i]
getConst _         = []

getAllConst = everything (++) (mkQ getConst)

Contrast this with the typical use of Generics requiring two classes, a traversal over the sum of products representation, and instantiate the class N times for the N types needing traversed. Where SYB, and indeed most generic systems, fall flat is in performance.

Caryopsis answered 12/11, 2014 at 19:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.