2015-07-12 64 views
1

对于这种情况有多困难,我感到有些莫名其妙。我已经看了看周围的计算器,但没有解决方案似乎对我工作正常。字符串插值,转义引号

我想要做什么:

val file = checkcache(fileName) 

file match 
{ 
    case Some(_) => {println(s"File $file found!"); file.get} 
    case None => createFile(fileName) 
} 

现在,这个工作完全正常,用于一个名为 “blubb” 文件,该文件已经存在于它outprints blubb发现

文件缓存

并返回文件。

现在,我想这是

文件 “blubb” 发现

所以我试着这样做:

case Some(_) => { println(s"File \" $file \" found!"); file.get} 

编译器会引发

“) '预计但字符串文字被发现。

为什么是这样的,以及如何正确地转义双引号,并且最好在$ file-variable之前或之前没有空的空格?

+1

有一个问题https://issues.scala-lang.org/browse/SI-6476和PR https://github.com/scala/scala/pull/4308所以也许在我们的一生中。 –

回答

6

使用三重引号:

scala> s"""File "$file" found!""" 
res0: String = File "blubb" found!