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

constant folding should be signaled with @inline, not final #7542

Closed
scabug opened this issue May 31, 2013 · 9 comments
Closed

constant folding should be signaled with @inline, not final #7542

scabug opened this issue May 31, 2013 · 9 comments
Labels
enhancement fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)

Comments

@scabug
Copy link

scabug commented May 31, 2013

The semantics of constant folding are a source of endless confusion, ignorance, and eventually consternation.

  • that one signals it should take place with "final", a word already triple-overloaded in meaning by java
  • that there can be no type annotation, a requirement which seems utterly arbitrary to someone not versed in the implementation

And even if you know all the rules, you have a good chance of being out of luck:

  • since you can't annotate the type, there is no way to see a Byte, Short, or Char inlined (as those types are never inferred for literals)
  • you are required to know implementation details to know what will actually be inlined, because superficially identical sources fall out differently, as here:
object Test {
  final val s1 = "abc"
  final val s2 = s"${s1}def"
  final val s3 = s1 + "def"
  def s4 = s2
  def s5 = s3
}

Is it reasonable to expect people to intuit that s5 has only a reference to string constant "abcdef", but s4 calls s2? I don't think so.

There is an obvious solution, which will also be the right kind of obvious to the scala programmer. If you want a constant inlined, mark it @inline.

We can do this immediately, keeping the final semantics as well for as long as desired, but ideally deprecating it in favor of @inline now.

@scabug
Copy link
Author

scabug commented May 31, 2013

Imported From: https://issues.scala-lang.org/browse/SI-7542?orig=1
Reporter: @paulp
See #7174

@scabug
Copy link
Author

scabug commented May 31, 2013

@retronym said:
The interaction with static annotations needs to be figured out.

Here's one more visible difference to the the current scheme that I noticed a while back in ThreeValues.

scala> object O { final val YES = 1; YES: Byte }

scala> object O { @inline val YES = 1; YES: Byte }
<console>:7: error: type mismatch;
 found   : Int
 required: Byte
       object O { @inline val YES = 1; YES: Byte }
                                       ^                                          ^

@scabug
Copy link
Author

scabug commented May 31, 2013

@soc said:
See also https://groups.google.com/d/topic/scala-internals/b95Y-GbXVGA/discussion.

This could also improve incremental compile time, because less classes need to be recompiled if a constant changes.

@scabug
Copy link
Author

scabug commented May 31, 2013

@soc said:
Language spec chapter 4.1 needs to be updated, if this is fixed.

@scabug
Copy link
Author

scabug commented May 31, 2013

@paulp said:
Using constant inlining to present the same literal as different types seems more in the curiosity category. I acknowledge it would be a "difference" to lose that ability...

@scabug
Copy link
Author

scabug commented May 31, 2013

@paulp said:
@soc - that is a good point, I assume that has been discussed but I didn't entirely take it in. The current semantics must create lots of bonus dependency arrows.

@scabug
Copy link
Author

scabug commented Aug 8, 2016

@SethTisue said (edited on Aug 26, 2016 6:43:19 PM UTC):
an alternative proposal (broader in scope): https://gist.github.com/xeno-by/9d7a709b1ba7c2ee64cfedcc5d264bd5 (Eugene at al's SIP on "Inline Definitions and Meta Expressions")

@scabug scabug added this to the 2.13.0-M1 milestone Apr 7, 2017
@adriaanm adriaanm modified the milestones: 2.13.0-M1, 2.13.0-M2 Apr 14, 2017
@szeiger szeiger modified the milestones: 2.13.0-M3, 2.13.0-M2 Jul 20, 2017
@adriaanm adriaanm modified the milestones: 2.13.0-M3, 2.13.0-M4 Jan 30, 2018
@lrytz lrytz modified the milestones: 2.13.0-M4, 2.13.0-M5 Apr 23, 2018
@adriaanm adriaanm modified the milestones: 2.13.0-M5, 2.13.0-RC1 Aug 8, 2018
@adriaanm adriaanm modified the milestones: 2.13.0-RC1, Backlog Nov 13, 2018
@scala scala deleted a comment from scabug Mar 31, 2019
@scala scala deleted a comment from scabug Mar 1, 2023
@scala scala deleted a comment from scabug Mar 1, 2023
@SethTisue
Copy link
Member

I think this is now out of scope to change in Scala 2.

Scala 3 has inline.

@SethTisue SethTisue removed this from the Backlog milestone Mar 1, 2023
@som-snytt
Copy link

The conclusion in 2013 was that it's easier to just rewrite Scala from scratch to support inline.

@SethTisue SethTisue added the fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/) label Mar 2, 2023
@SethTisue SethTisue closed this as not planned Won't fix, can't repro, duplicate, stale Mar 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement fixed in Scala 3 This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)
Projects
None yet
Development

No branches or pull requests

6 participants