String interpolation in Scala?

后端 未结 5 1224
粉色の甜心
粉色の甜心 2021-01-04 04:43

I wanted to ask if there is any type of string interpolation in Scala. I have made a search on the subject but \'till now I have found that there is no string interpolation.

5条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-01-04 04:52

    This days (Dec. 2016, Scala 2.12, five years later), you can write your own string interpolation.
    See co.ntextu.al

    Contextual is a small Scala library which allows you to define your own string interpolators—prefixed string literals like uri"https://google.com" which determine how they should be evaluated, at runtime and at compile-time, while only writing very ordinary user code: no macros!

    For instance, contextual/examples/email.scala allows to check at compile time the validity of an email address.

    import contextual.examples.email._
    email"""info@scala.world"""
    
    import contextual.examples.email._
    email"""john.smith@hotmail.com"""
    

提交回复
热议问题