Skip to content

Add enum example in documentation and a test for 'case class cannot extend enum' #5014

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 3 commits into from
Aug 25, 2018
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
14 changes: 14 additions & 0 deletions compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,20 @@ class ErrorMessagesTests extends ErrorMessagesTest {
checkMessagesAfter(FrontEnd.name)("""class Foo""")
.expectNoErrors

@Test def caseClassExtendsEnum =
checkMessagesAfter(RefChecks.name) {
"""
|enum Foo {}
|case class Bar() extends Foo
""".stripMargin
}
.expect { (ictx, messages) ⇒
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val errorMsg = messages.head
assertEquals("normal case class Bar in package <empty> cannot extend an enum", errorMsg.msg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW: we usually don't test strings but that's the correct thing *as long as we don't switch to error messages here (#1589). @skvithalani are you interested in doing that in a followup? Accepting this meanwhile.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure . I will take that up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Beyond the pre-labeled issues, ones in parser/desugar tend to be more approachable.

}

@Test def typeMismatch =
checkMessagesAfter(FrontEnd.name) {
"""
Expand Down
29 changes: 29 additions & 0 deletions docs/docs/reference/enums/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,35 @@ object Planet {
println(s"Your weight on $p is ${p.surfaceWeight(mass)}")
}
}
```
### A specific use case of enum

Two enums can be combined into a different type as shown below:

```scala

trait Animal
object Animal {
def enumValues: Iterable[Animal] = Dog.enumValues ++ Cat.enumValues
def enumValueNamed = Dog.enumValueNamed ++ Cat.enumValueNamed
}

enum Dog extends Animal {
case GermanShepherd, Labrador, Boxer
}

enum Cat extends Animal {
case PersianCat, Ragdoll, ScottishFold
}

object Main {
def main(args: Array[String]): Unit = {
val values: Iterable[Animal] = Animal.enumValues
val boxer_dog: Animal = Animal.enumValueNamed("Boxer")
val ragdoll_cat: Animal = Animal.enumValueNamed("Ragdoll")
}
}

```

### Implementation
Expand Down
2 changes: 2 additions & 0 deletions tests/pending/neg/i5008
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
enum Foo {}
enum Bar extends Foo {} // error