Line breaks in raw lines are platform dependent?

Line breaks in raw lines are platform dependent?

val a = "one\ntwo"; val b = """one two""" println(a == b) 

In other words, is the statement of println above guaranteed to print true or not?

+5
source share
2 answers

Unfortunately, I could not find sources that specifically point to this. This is similar to what should be in the documents.

However, in IntelliJ, the intent action converts the raw string to a regular string. I think it should be safe to assume that this action should not change the meaning of your code (or if so, then it should be reported as an error). If you try this on a raw line with a new line in it, you will see that it replaces the new line with the \n character.

You can see the source of the action here and the test for it, which expects it to convert the new string to \n here (before) and here (after) .


Change here the comment on the raw string problem, which (as I understand it) claims that the raw string has \n line endings in it.

+1
source

I would not assume that a and b are equal. Spec talks about the source string literals:

Kotlin has two types of string literals: escaped strings that can have escaped characters in them and raw strings that can contain newline characters and arbitrary text.

  • The specification does not say the same
  • The spectrum makes a distinction between "new line" and "free text"
  • And in the @ zsmb13 link to Kotlinโ€™s discussion, the engineer Kotlin says that the compiler does not save newline characters in raw lines (??).

So, if I wrote code to work on different platforms, I would be explicit (at least until they are).

0
source

Source: https://habr.com/ru/post/1272791/


All Articles