justinwoo / purescript-simple-json

A simple Purescript JSON library that uses types automatically
http://purescript-simple-json.readthedocs.io
MIT License
133 stars 45 forks source link

Cutting down on boilerplate #28

Closed i-am-the-slime closed 6 years ago

i-am-the-slime commented 6 years ago

Hey there,

I am fairly new to Purescript, and I don't know if this is possible:

I find myself writing newtype declarations like this:

newtype ImageUrl = ImageUrl String
derive instance ntImageUrl :: Newtype ImageUrl _
derive instance genImageUrl ∷ Generic ImageUrl _
instance showSite :: Show ImageUrl where
  show = genericShow
derive newtype instance writeForeignImageUrl :: WriteForeign ImageUrl
derive newtype instance readForeignImageUrl :: ReadForeign ImageUrl

Now I could make that into a snippet in my editor but it would start to feel an awful lot like: Generate Getters/Setters in Java.

Is there any way to achieve this with less boilerplate? Maybe... TemplatePurescript 😮 ?

justinwoo commented 6 years ago

Depends on how you're using the show instance, but you could newtype derive that too :stuck_out_tongue:

But I think the thing is that the instance names are used in the actual generated code, so there's no short newtype deriving form like in Haskell. You might very well set up some kind of codegen pipeline too, but maybe best is to get comfy with vim or spacemacs and copy-paste + region sub/match group edit/iedit and such (i.e. difference between selecting an area with visual mode and using :s/What/Thing/, doing /What and then using cgnThing on a match and hitting . to repeat, or putting your cursor on a symbol and doing spc s e and using shift-J to select the lines to apply iedit to)

i-am-the-slime commented 6 years ago

Alright, I'll do it with some editor magic :stuck_out_tongue_closed_eyes: