[Haskell-beginners] Design help to add imports to a (Java) template
Hilco Wijbenga
hilco.wijbenga at gmail.com
Mon Jul 2 20:03:07 UTC 2018
Hi all,
I'm working on a Haskell (Eta) tool to generate Java ADTs.
I have the following definitions:
data Line = Line Int Text [JavaImport] -- The Int represents the
indentation level of this line.
data Template
= None
| One Line
| Many [Line]
| Template (Reader Configuration Template)
I then run it like this (incomplete, I hope you get the idea):
main :: IO ()
main = putStr $ replace (importsGoHereKey `append` "\n") importsAsText
templateAsText
where
config = Configuration (Copyright 2018 "Me") USE_GETTERS
point' = javaClassTemplate point
importsAsText = runReader (mapReader javaImportsToText
(javaImports point')) config
templateAsText = runReader (toText tab point') config
Essentially, I use the template twice: once to extract the Java
imports and once to generate the result Text. Then I replace a magic
string in the result Text with the import code. It works but makes me
sad. ;-)
I will probably pull out the JavaImport list from Line and use
something like State to keep track of the necessary imports. (Now that
I understand Reader, maybe State is no longer a bridge too far.)
My main issue is how to avoid the magic string. I thought about adding
an extra entry to Template [something like Imports ((Set JavaImport)
-> Text or maybe Template)] but then I don't know how to implement
"instance Monoid Template". Similarly if I use something Reader like.
Any design advice or ideas would be appreciated. If it's of any help,
the full code is at [1] (it's tiny). Any and all comments are welcome,
by the way: I'm still a Haskell beginner.
Cheers,
Hilco
[1] https://github.com/hilcode/java-algebraic-data-types
More information about the Beginners
mailing list