Search code examples
scalascalikejdbc

Difference between $param and ${param} in scalikejdbc


When using scalikejdbc, is there any difference between:

val test = "test"
sql"""SELECT $test from mytable"""

and

val test = "test"
sql"""SELECT ${test} from myable"""

E.g. will both use prepared-statement-like variable replacement?

All the examples I've seen so far use the latter syntax, however I've seen some code that uses the former (which seems slightly prettier to me), which is why I was wondering if there's any reason to surround your variable names with {}


Solution

  • Much like Strings, you can use "$hello" to interpolate a variable and "${hello}" to interpolate an expression.

    If you have a case class Foo(a: Int) and an instance val foo = Foo(42), you can interpolate "${foo.a}" directly, or you can create an intermediate value, val fooA = foo.a and interpolate that intermediate: "$fooA".