newClosureGeneric :: CodeGen a -> CodeGen (a, CgState)
newClosureGeneric genCode = do
  state0@CgState
    { cgAccessFlags = a
    , cgMethodDefs = b
    , cgFieldDefs = c
    , cgClassName = d
    , cgSuperClassName = e } <- get
  -- TODO: Ensure the proper state is reset.
  modify $ \s -> s { cgAccessFlags = [Public, Super, Final]
                   , cgMethodDefs = []
                   , cgFieldDefs = []
                   , cgClassName = mempty
                   , cgSuperClassName = Nothing }
  result <- genCode
  state1 <- get
  let compiledClosure = classFromCgState state1
  -- TODO: Ensure the state is restored properly
  modify $ \s -> s { cgAccessFlags = a
                   , cgMethodDefs = b
                   , cgFieldDefs = c
                   , cgClassName = d
                   , cgSuperClassName = e }
  addCompiledClosure compiledClosure
  return (result, state1)
newClosureGeneric :: CodeGen a -> CodeGen (a, CgState)
newClosureGeneric genCode = do
  state0@CgState
    { cgAccessFlags = a
    , cgMethodDefs = b
    , cgFieldDefs = c
    , cgClassName = d
    , cgSuperClassName = e } <- get
  -- TODO: Ensure the proper state is reset.
  modify $ \s -> s { cgAccessFlags = [Public, Super, Final]
                   , cgMethodDefs = []
                   , cgFieldDefs = []
                   , cgClassName = mempty
                   , cgSuperClassName = Nothing }
  result <- genCode
  state1 <- get
  let compiledClosure = classFromCgState state1
  -- TODO: Ensure the state is restored properly
  modify $ \s -> s { cgAccessFlags = a
                   , cgMethodDefs = b
                   , cgFieldDefs = c
                   , cgClassName = d
                   , cgSuperClassName = e }
  addCompiledClosure compiledClosure
  return (result, state1)
newClosureGeneric genCode = do
  state0@CgState
    { cgAccessFlags = a
    , cgMethodDefs = b
    , cgFieldDefs = c
    , cgClassName = d
    , cgSuperClassName = e } <- get
  -- TODO: Ensure the proper state is reset.
  modify $ \s -> s { cgAccessFlags = [Public, Super, Final]
                   , cgMethodDefs = []
                   , cgFieldDefs = []
                   , cgClassName = mempty
                   , cgSuperClassName = Nothing }
  result <- genCode
  state1 <- get
  let compiledClosure = classFromCgState state1
  -- TODO: Ensure the state is restored properly
  modify $ \s -> s { cgAccessFlags = a
                   , cgMethodDefs = b
                   , cgFieldDefs = c
                   , cgClassName = d
                   , cgSuperClassName = e }
  addCompiledClosure compiledClosure
  return (result, state1)