1

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 {}

erip
  • 16,374
  • 11
  • 66
  • 121
Dreamer
  • 13
  • 2

1 Answers1

1

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".

erip
  • 16,374
  • 11
  • 66
  • 121
  • Thanks erip! :) So I take it they are identical in the use case posted above, but examples usually use {} because it allows for more use cases. – Dreamer Aug 11 '17 at 02:24
  • I'd say drop the curlies unless it becomes inconvenient to create an intermediate, but that's just my preference. – erip Aug 11 '17 at 17:21