Skip to content

document enum cases cannot use private val as part of definition #11249

Closed
@ekrich

Description

@ekrich

Compiler version

Scala 3.0.0-M3

Minimized example

enum Planet(mass: Double, radius: Double) extends java.lang.Enum[Planet]:
   private final val G = 6.67300E-11
   private final val mm = 3.303e+23
  
   def surfaceGravity = G * mass / (radius * radius)
   def surfaceWeight(otherMass: Double) = otherMass * surfaceGravity

   case Mercury extends Planet(mm, 2.4397e6)
   case Venus   extends Planet(4.869e+24, 6.0518e6)
   case Earth   extends Planet(5.976e+24, 6.37814e6)
   case Mars    extends Planet(6.421e+23, 3.3972e6)
   case Jupiter extends Planet(1.9e+27,   7.1492e7)
   case Saturn  extends Planet(5.688e+26, 6.0268e7)
   case Uranus  extends Planet(8.686e+25, 2.5559e7)
   case Neptune extends Planet(1.024e+26, 2.4746e7)
end Planet

import Planet._

@main def trySomeThings: Unit =
  val earthWeight = 120.toDouble
  val mass = earthWeight / Earth.surfaceGravity
  for p <- values do
    println(s"Your weight on $p is ${p.surfaceWeight(mass)}")
end trySomeThings
  

Output

The output on Scastie was Not found: mm. The following is a real world output.

[error] -- [E006] Not Found Error: /Users/eric/workspace/sjavatime/sjavatime/shared/src/main/scala-3/java/time/temporal/ChronoUnit.scala:12:50 
[error] 12 |  case NANOS extends ChronoUnit(Duration.OneNano, isTimeBasedFlag)
[error]    |                                                  ^^^^^^^^^^^^^^^
[error]    |                                              Not found: isTimeBasedFlag

Expectation

It should be possible to work. This can make the code much more readable.

enum ChronoUnit(duration: Duration,
    flags: Int) extends jl.Enum[ChronoUnit] with TemporalUnit {
  // TODO: find out way this doesn't work
  private final val isTimeBasedFlag = 1
  // private final val isDateBasedFlag = 2

  case NANOS extends ChronoUnit(Duration.OneNano, isTimeBasedFlag)

  case MICROS extends ChronoUnit(Duration.OneMicro, 1)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions