3

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

6
  • 1
    What is it exactly that you're trying to do? Commented Apr 28, 2011 at 19:08
  • @Jean-Philippe Pellet: I need to generate a rather large SQL function in my Scala code, with replacement of parts of the function with calculated pieces (like args above). Commented Apr 28, 2011 at 19:11
  • I really like your idea! I often had the same problem, not only with SQL. Sometimes one constructs a fat string with format having many many format arguments. Commented Apr 28, 2011 at 19:44
  • @Peter Schmitz: I am also putting Scala code into the inner braces that actually calculates the columns names, etc.: {columns.map(column => column.name).mkString(",\n")} -- see update in the question. Commented Apr 28, 2011 at 20:28
  • 1
    possible duplicate of Better String formatting in Scala Commented Apr 28, 2011 at 20:36

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I've written a wrapper for velocity two years ago that fits the use case of the question. kneissl.eu/open-source/scala-velocity
@mkneissl: Almost gets around the lack of a preprocessor. :-)
2

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

Comments

2

Good news! Scala 2.10.0 introduced real, functional string interpolation!

The docs are available here: http://docs.scala-lang.org/overviews/core/string-interpolation.html

Here's a quick sample:

In Python, I used to do things like:

print "%(from)s -> %(to)s" % {"from": foo, "to": bar}

now, in Scala 2.10.0+, we can do this!

val from = "Foo"
val to = 256
println(s"$from -> $to")  // Prints: Foo -> 256

There's also some format string support as well, which is pretty awesome:

val from = 10.00  // USD
val to = 984.30  // JPY
println(f"$$$from%.2f -> $to%.2fJPY")  // Prints: $10.00 -> 984.30JPY

Since the second example has some minimal type expressiveness, it also gives us some basic type checking as well!

val from = 10.00
println(f"$$$from%d") // <-- Type error! Found "Double", required "Int"!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.