3

I went through the answer on Purpose of Scala's Symbol?

I want to create a Symbol but it has space in it.

Ex:

val s = "Hello World"
val syb = 'Hello World

syb gives compile time error. Is it possible to create a Symbol with spaces?

Shaido
  • 27,497
  • 23
  • 70
  • 73
coder25
  • 2,363
  • 12
  • 57
  • 104
  • 1
    `'foo` is simply syntactic sugar for `Symbol("foo")`. And that's not an implementation detail, AFAIK it is explicitly defined that way in the Scala Language Specification. – Jörg W Mittag Aug 16 '17 at 13:35

1 Answers1

5

It is possible to create a Symbol with spaces with the Symbol.apply method:

scala> val syb = Symbol("Hello World")
syb: Symbol = 'Hello World
Shaido
  • 27,497
  • 23
  • 70
  • 73
  • 1
    Rather annoying that the Symbol class' `toString` doesn't have a special case for these symbols. Printing `'Hello World` is incredibly ugly. – Silvio Mayolo Aug 16 '17 at 04:03