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

[ValueEnum] When using inheritance, Compile error: not all of the members have a literal/constant 'value:String' declaration #294

Open
ajaychandran opened this issue Oct 3, 2020 · 1 comment

Comments

@ajaychandran
Copy link

ajaychandran commented Oct 3, 2020

Version: 1.6.1

import enumeratum.values.{ StringEnum, StringEnumEntry }

import scala.collection.immutable

sealed abstract class Day extends StringEnumEntry with Product with Serializable

object Day extends StringEnum[Day] {

  sealed abstract class Weekend(val value: String) extends Day
  sealed abstract class Workday(val value: String) extends Day

  def values: immutable.IndexedSeq[Day] = findValues

  final case object Sun extends Weekend("Sun")
  final case object Mon extends Workday("Mon")
  final case object Tue extends Workday("Tue")
  final case object Wed extends Workday("Wed")
  final case object Thu extends Workday("Thu")
  final case object Fri extends Workday("Fri")
  final case object Sat extends Weekend("Sat")

  final object Weekend extends StringEnum[Weekend] {

    def values: immutable.IndexedSeq[Weekend] = Day.values.collect { case day: Weekend => day }
  }

  final object Workday extends StringEnum[Workday] {

    def values: immutable.IndexedSeq[Workday] = Day.values.collect { case day: Workday => day }
  }
}
[error] It looks like not all of the members have a literal/constant 'value:String' declaration, namely: object Sun, object Mon, object Tue, object Wed, object Thu, object Fri, object Sat.
[error] 
[error] This can happen if:
[error] 
[error] - The aforementioned members have their `value` supplied by a variable, or otherwise defined as a method
[error] 
[error] If none of the above apply to your case, it's likely you have discovered an issue with Enumeratum, so please file an issue :)
[error]          
[error]   val values: immutable.IndexedSeq[Day] = findValues
@lloydmeta
Copy link
Owner

lloydmeta commented Oct 6, 2020

Ugh, I think the reason this is happening is because we need to check for value being implemented as a literal on Day when expanding findValues, but in this case, it isn't and won't be: it's implemented as a literal on Weekday or Workday instead. Not 100% sure if this is something we can overcome in the macro itself without breaking guarantees about "literal-ness"; haven't really checked.

As workaround, maybe something like this might help

import enumeratum.values.{ StringEnum, StringEnumEntry }

import scala.collection.immutable

sealed abstract class Day extends StringEnumEntry with Product with Serializable

object Day extends StringEnum[Day] {

  sealed abstract class Weekend(val value: String) extends Day
  sealed abstract class Workday(val value: String) extends Day

  val values: immutable.IndexedSeq[Day] = Weekend.values ++ Workday.values
  final object Weekend extends StringEnum[Weekend] {
    
      final case object Sun extends Weekend("Sun")
      final case object Sat extends Weekend("Sat")

    val values: immutable.IndexedSeq[Weekend] = findValues
  }

  final object Workday extends StringEnum[Workday] {
    
    final case object Mon extends Workday("Mon")
    final case object Tue extends Workday("Tue")
    final case object Wed extends Workday("Wed")
    final case object Thu extends Workday("Thu")
    final case object Fri extends Workday("Fri")

    val values: immutable.IndexedSeq[Workday] = findValues
  }
}

On Scastie

@lloydmeta lloydmeta changed the title Compile error: not all of the members have a literal/constant 'value:String' declaration [ValueEnum] When using inheritance, Compile error: not all of the members have a literal/constant 'value:String' declaration Oct 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants