Search code examples
stringscalanamed-parameters

Scala String format named parameters (Winner: Ugliest Code)


I came up with a trick to use named parameters in Scala. Is there a better way? What are the downsides?

<x>
  |CREATE OR REPLACE FUNCTION myFunction({columns.map(column => column.name).
                                          mkString(",\n")})
  |RETURNS BOOLEAN AS $$
  |BEGIN
  | -- more stuff
  |END;
  |$$ LANGUAGE 'plpgsql';
  |</x>.text.stripMargin

Watch out for ampersands in the XML body; they need to be "quoted" as &amp; or placed in braces like {"&"}. Do I win a prize for ugliest code? :-)


Solution

  • I think that if you need a string formater on this scale, you need a Builder or a templating engine, like Velocity. Incidentally, I've found Scala's good for builders and DSLs.