Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9177751
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T17:23:48+00:00 2026-06-17T17:23:48+00:00

I want to create a generic class that holds the value of an enumeration,

  • 0

I want to create a generic class that holds the value of an enumeration, and also allows access to the possible values of the enumeration. Think of a property editor for example – you need to know the current value of the property, and you also need to be able to know what other values are legal for the property. And the type of enumeration should not be known in advance, you should be able to work with any kind of enumeration.

My first thought was something like this:

class EnumerationProperty[T <: Enumeration](value:T)

However, that doesn’t work because for enumerations T isn’t a type, it’s an object. Other variations I have tried are:

class EnumerationProperty[T <: Enumeration](value:T.Value)
class EnumerationProperty[T <: Enumeration.Value](value:T)

(I won’t go into the details of why these don’t work because I suspect the reasons aren’t interesting.)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T17:23:49+00:00Added an answer on June 17, 2026 at 5:23 pm

    In your use case (a property editor) I think the best solution is to rely on scala reflection available in scala 2.10. Given a class’s TypeTag, you can get the full type (without erasure) of all of its members plus their values, and from that populate your property editor. For enumerations, use the TypeTag to get their values using the following method:
    Using Scala 2.10 reflection how can I list the values of Enumeration?


    Now, maybe you don’t want or can’t use scala reflection, and from now on I’ll suppose this is the case. If that is true then you are opening a whole can of worm 🙂
    TL;DR: This is not possible using a standard Enumeration, so you’ll probably have to explcitly wrap the enumeration values as shown in Ptharien’s Flame’s answer (or roll your own fork of Enumeration). Below I detail all my attempts, please bear with me.

    Unfortunately, and for some unknown reason to me (though I suspect it has to do with serialization issues), Enumeration.Value has no field pointing to its Enumeration instance. Given how Enumeration is implemented, this would be trivial to implement, but of course wehave no say, short of forking Enumeration and modifying our version (which is actually what I did for this very purpose, plus to add proper support for serialization and reflection – but I diggress).

    If we can’t modify Enumeration, maybe we can just extend it? Looking at the implementation again, something like this would seem to work:

    class EnumerationEx extends Enumeration {
      protected class ValEx(i: Int, name: String) extends Val(i, name) {
        @transient val enum: Enumeration = EnumerationEx.this
      }
      override protected def Value(i: Int, name: String): Value = new ValEx(i, name)  
    }
    
    object Colors extends EnumerationEx {
      val Red, Green, Blue = Value
    }
    

    The downside would be that it only works for enumerations that explicitly extend EnumerationEx instead of Enumeration, but it would be better than nothing.

    Unfortunately, this does not compile simply because def Value ... is declared final in Enumeration so there is no way to override it. (Note again that forking Enumeration would allow to circunvent this. Actually, why not do it, as we are already down the path of using a custom Enumeration anwyay. I’ll let you judge).

    So here is another take on it:

    class EnumerationEx extends Enumeration {
      class ValueWithEnum( inner: Value ) {
        @transient val enum: Enumeration = EnumerationEx.this
      }
      implicit def valueToValue( value: Value ): ValueWithEnum = new ValueWithEnum( value )
    }
    

    And indeed it works as expected. Or so it seems.

    scala> object Colors extends EnumerationEx {
         |   val Red, Green, Blue = Value
         | }
    defined module Colors
    
    scala> val red = Colors.Red
    red: Colors.Value = Red
    
    scala> red.enum.values
    res58: Enumeration#ValueSet = Colors.ValueSet(Red, Green, Blue)
    

    Hooray? Well no, because the conversion from Value to ValueWithEnum is done only when accessing red.enum, not at the time of instantiation of the Colors enumeration. In other words, when calling enum the compiler needs to know the exact static type of the enumeration (the compiler must statically know that red’s type is Colors.Value, and not just Enumeration# Value). And in the use case you mention (a property editor) you can only rely to java reflection (I already assumed that you won’t use scala reflection) to get the type of an enumeration value, and java reflection will only give you Enumeration#Val (which extends Enumeration#Value) as the type of Colors.Red. So basically you are stuck here.
    Your best bet is definitly to use scala reflection in the first place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to create a generic class that takes a type parameter and restrict
I want to create the generic class template that uses internally a specific container
i want to create a generic class that would accept T. T is an
I want to create a generic method to serizlize a class to text (for
I want to create a generic html table to excel file view that can
Hi i want to create a generic style for pin button. <Window x:Class=TooglePinButtonStyle.MainWindow xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation
I want to create a generic class, whose builder would not return an instance
I want to create a class that has several public variables and methods,but behaves
I'am trying to create a generic class that contains a list of generic type
I want to create a generic class which helps me to compare two dictionary

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.