Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

String interpolation with constants #8650

Closed
scabug opened this issue Jun 6, 2014 · 5 comments · Fixed by scala/scala#10364
Closed

String interpolation with constants #8650

scabug opened this issue Jun 6, 2014 · 5 comments · Fixed by scala/scala#10364

Comments

@scabug
Copy link

scabug commented Jun 6, 2014

String interpolation can't be a constant?

final val x = "hello" // constant
final val y = s"$x world" // not constant
@scabug
Copy link
Author

scabug commented Jun 6, 2014

Imported From: https://issues.scala-lang.org/browse/SI-8650?orig=1
Reporter: @okomok
Affected Versions: 2.11.1
See #8608

@scabug
Copy link
Author

scabug commented Jun 9, 2014

@som-snytt said:
Contributing this to the f interpolator. The interesting twist is indexed args {code}f"$x%f == %<.3f == %1$$.3f"{code}

@scabug
Copy link
Author

scabug commented Jan 31, 2015

@som-snytt said (edited on Feb 13, 2015 9:54:44 AM UTC):
scala/scala#4316

@som-snytt
Copy link

Not yet:

scala 2.13.0> final val x = "hello"
x: String("hello") = hello

scala 2.13.0> val xx: "hello" = x
xx: "hello" = hello

scala 2.13.0> final val y = s"$x world"
y: String = hello world

scala 2.13.0> val yy: "hello world" = y
                                      ^
              error: type mismatch;
               found   : y.type (with underlying type String)
               required: "hello world"

scala 2.13.0> @deprecated(y,"") def f = 42
f: Int

scala 2.13.0> f
              ^
              warning: method f is deprecated
res0: Int = 42

@som-snytt
Copy link

sjrd mentions the limitation of platform-dependent toString at scala/scala#10364 (comment)

We should exclude Unit, Float and Double here, because they do not have a portable toString() representation. If we convert them to strings in the compiler, that will always use the JVM toString(), so the result won't be correct on other platforms.

Similar issue here, but a bit worse: Byte and Short must be excluded as well, in addition to Unit, Float and Double. In theory Byte and Short should only be excluded for %x, %X and %o, while Unit, Float and Double should only be excluded for %s, but it's probably easier to excluded all of them all the time.

Surely if they are relying on toString, they get their just desserts. As opposed to getting just dessert.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants