Skip to content

Added enum sample code in Scala 3 #1388

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

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.baeldung.scala.enumerations

// This is Scala 2 based enumeration. It still works in Scala 3 for compatibility.
// However, this might become unsupported in future release. In scala 3, use the keyword `enum`
object Fingers extends Enumeration {
type Finger = Value

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.baeldung.scala.enumerations.scala3

enum Fingers(val height: Double) {
case Thumb extends Fingers(1)
case Index extends Fingers(4)
case Middle extends Fingers(4.1)
case Ring extends Fingers(3.2)
case Little extends Fingers(0.5)
def heightInCms(): Double = height * 2.54
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.baeldung.scala.enumerations.scala3

import org.junit.Test
import org.junit.Assert.assertTrue

class FingersUnitTest {

@Test
def givenAFinger_getTheHeight(): Unit = {
val Thumb = Fingers.Thumb
assertTrue(Thumb.height == 1d)
}

@Test
def givenAFinger_getTheHeightInCm(): Unit = {
val Thumb = Fingers.Thumb
assertTrue(Thumb.heightInCms() == 2.54d)
}

}