Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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? :-)

like image 358
Ralph Avatar asked Apr 08 '26 07:04

Ralph


2 Answers

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.

like image 106
sblundy Avatar answered Apr 10 '26 00:04

sblundy


If you don't mind a compiler plugin, try Johannes Rudolph's Scala Enhanced Strings. I like it a lot.

like image 35
Alex Cruise Avatar answered Apr 09 '26 22:04

Alex Cruise