0

My question is similar to Printing Unicode from Scala interpreter and scala default encoding in windows shell, but goes into more detail WRT different behavior of Scala (version 2.12.2) vs. Groovy (version 2.4.7).

I have the following code snippet (which happens to be both valid Scala and Groovy code):

println("└─ ");
println("├─ ");
println(System.getProperty("file.encoding"));

When running the scala interpreter on it the Windows terminal (all of cmd, PowerShell, Git Bash / mintty) it shows

$ scala a.scala
??
??
Cp1252

However, running the exact same code through the groovy interpreter it shows:

$ groovy a.scala
└─
├─
Cp1252

My understanding from reading the answers to the linked questions is that the output is (solely) dependent on the value of the file.encoding system property. If so, how can the output be different between Scala and Groovy then if the used file.encoding is the same?

Edit: This answer seems to confirm it's an issue with the encoding used by the Scala compiler to read the source code file, which seems to default to whatever file.encoding is set to, even if the file clearly is UTF-8 encoded. I'm still wondering why Scala is not as smart as Groovy here...

Edit 2: I can work around the issue by running

$ scala -Dfile.encoding=UTF8 a.scala
└─
├─
UTF8

but still this dos not answer the question why Groovy does get it right despite file.encoding being set to Cp1252.

sschuberth
  • 28,386
  • 6
  • 101
  • 146

1 Answers1

2

I think it's connected with different source encoding during compilation step

try this instead of println("├─ ");

println("\u251C\u2500");

or for groovy there is a parameter --encoding to specify the encoding of the source files.

quite sure for scala should be the same parameter

finally better not to use non-ASCII characters in your source code.

I used this service to convert chars (but any ide can do it for you)

daggett
  • 26,404
  • 3
  • 40
  • 56
  • Good idea that it's related to the encoding the compiler uses to read the file. However, your code still gives `??` for Scala for me, and now for Groovy too. – sschuberth Jun 09 '17 at 10:47
  • When reading through [this answer](https://stackoverflow.com/a/23226994/1127485) it indeed seems you're right, but it's still awkward that Scala would use `file.encoding` even if the file clearly is UTF-8 encoded. – sschuberth Jun 09 '17 at 10:51
  • Ok, another point could be is `console encoding`. for me (on `windows`) if i set console encoding to `1252` with command `chcp 1252` then i'll see `??` in output. and if i choose `chcp 437` then i'll see `├─`. – daggett Jun 09 '17 at 11:45
  • and check out this: https://stackoverflow.com/questions/1259084/what-encoding-code-page-is-cmd-exe-using – daggett Jun 09 '17 at 11:48
  • Yeah, I've read that (or similar) answers, but these refer to the general console font, which cannot be the cause if it works with Groovy, but does not work with Scala in the same console window. – sschuberth Jun 09 '17 at 12:09
  • i just installed `scala 2.12.2` and i have `groovy 2.4.11` and your code executed fine in the same console with same output. – daggett Jun 09 '17 at 12:32