Scala raw strings error in unicode escape

故事扮演 提交于 2019-11-28 08:11:59

问题


In a Scala String need to include this literal verbatim: \usepackage{x}. Thus, desired would be that for

val s = """ ... \usepackage{X} ... """

println(s)
... \usepackage{X} ...

Attempts so far include,

scala> """\usepackage{X}"""
<console>:1: error: error in unicode escape
       """\usepackage{X}"""
            ^

scala> raw"""\usepackage{X}"""
<console>:1: error: error in unicode escape
       raw"""\usepackage{X}"""
               ^

Single double-quoted strings prove unsuccessful as well.

Following http://docs.scala-lang.org/overviews/core/string-interpolation.html , a working example includes

scala> raw"a\nb"
res1: String = a\nb

which does not cover unicode cases.


回答1:


You appear to be facing issue SI-4706: Unicode literal syntax thwarts common use cases for triple-quotes.

In Scala, unicode escape sequences are processed not only inside character or string literals. It may not be obvious that the following code would work:

scala> 5 \u002B 10
res0: Int = 15

Unfortunately, there doesn't seem to be a good way around this if you don't want to disable unicode escapes completely (-Xno-uescape). One of workarounds suggested in the issue is separating the backslash character:

scala> """\""" + """usepackage{X}"""
res1: String = \usepackage{X}


来源:https://stackoverflow.com/questions/24058549/scala-raw-strings-error-in-unicode-escape

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!