question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Enum case can't be defined in terms of itself

See original GitHub issue

Compiler version

3.0.0-M3

Minimized code

enum Opt[T] {
  case Nn extends Opt[Nothing] with Comparable[Nn.type]

  def compareTo(nn: Nn.type) = 0
}

Output

2 |  case Nn extends Opt[Nothing] with Comparable[Nn.type]
  |                                               ^
  |                                             Recursive value Nn needs type

Expectation

I expected it to work much like the Scala 2 equivalent:

sealed trait Opt[T]
case object Nn extends Opt[Nothing] with Comparable[Nn.type] {
  def compareTo(nn: Nn.type) = 0
}

Btw, this error was also mentioned in #11249 but the original issue there is about vals.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
LPTKcommented, Mar 29, 2021

Weird, I would have sworn that this worked in Scala 2… 🤔

A workaround that works in both Scala 2 and Scala 3 (!):

abstract class Module {
  type Nn >: Nn.type <: Nn.type
  sealed trait Opt[T]
  object Nn extends Opt[Nn] with Comparable[Nn] {
    def compareTo(nn: Nn) = 0
  }
}
object Module extends Module {
  type Nn = Nn.type
}

In fact, in Scala 3 we can simplify it to just:

object Module {
  type Nn >: Nn.type <: Nn.type
  sealed trait Opt[T]
  object Nn extends Opt[Nn] with Comparable[Nn] {
    def compareTo(nn: Nn) = 0
  }
}
0reactions
bishaboshacommented, Sep 21, 2021

@yuriy-yarosh your example is a duplicate of #9177 which was closed 😕

Read more comments on GitHub >

github_iconTop Results From Across the Web

Can you define an enum to represent values explicitly known ...
The trick is to define one additional case, unknown with an associated value of type RawValue . This new case handles all the...
Read more >
for case enum syntax | Apple Developer Forums
for case .MyEnumCase (let value) in enumValues. But tests in Xcode 7 show that this sort of thing won't compile (with suitable declaration...
Read more >
Access control for enum cases - Pitches - Swift Forums
Introduction. Today, Swift doesn't allow access control modifiers on enum cases, unlike properties, methods and other kinds of declarations.
Read more >
Why Swift Enums with Associated Values Cannot Have a Raw ...
The Problem with Associated Values​​ We had to do this because Swift doesn't allow us to have both: raw values and associated values...
Read more >
Swift Enumerations - AndyBargh.com
When defining enumeration cases, each case within the enumeration should also be unique. For example we couldn't define two cases called red within...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found