简体   繁体   中英

How is the compile-time magic of the Play framework's SQL operator working?

I am very impressed with the compile-time safety of Scala code like this:

val searchCountryPrefix:String = ....

val sql = SQL"""
  select
    cn.name, cn.indepyear, c.name
  from
    city c
    join country cn on c.id = cn.capital
  where
    cn.name like $searchCountryPrefix
  """

...because if I make a typo in the string, and eg use something wrong:

  ...
  where
    cn.name like $searchCountyPrefix -- instead of country, it says county
  """

...then the Scala compiler catches it and reports it.

I've never seen such compile-time magic in any other language except Lisp - how is the Play framework doing this?

And more importantly, can I do such magic compile-time string checking in my own libraries?

Thanks in advance for any help.

This is not specific to Play, it is a feature of Scala. You can learn more about String interpolation here: http://docs.scala-lang.org/overviews/core/string-interpolation.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM