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 8942401
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T11:30:21+00:00 2026-06-15T11:30:21+00:00

I just wanted to know if it is possible to iterate over a sealed

  • 0

I just wanted to know if it is possible to iterate over a sealed trait in Scala?
If not, why is it not possible? Since the trait is sealed it should be possible no?

What I want to do is something like that:

sealed trait ResizedImageKey {

  /**
   * Get the dimensions to use on the resized image associated with this key
   */
  def getDimension(originalDimension: Dimension): Dimension

}

case class Dimension(width: Int,  height: Int)

case object Large extends ResizedImageKey {
  def getDimension(originalDimension: Dimension) = Dimension(1000,1000)
}

case object Medium extends ResizedImageKey{
  def getDimension(originalDimension: Dimension) = Dimension(500,500)
}

case object Small extends ResizedImageKey{
  def getDimension(originalDimension: Dimension) = Dimension(100,100)
}

What I want can be done in Java by giving an implementation to the enum values. Is there an equivalent in Scala?

  • 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-15T11:30:23+00:00Added an answer on June 15, 2026 at 11:30 am

    This is actually in my opinion an appropriate use case for 2.10 macros: you want access to information that you know the compiler has, but isn’t exposing, and macros give you a (reasonably) easy way to peek inside. See my answer here for a related (but now slightly out-of-date) example, or just use something like this:

    import language.experimental.macros
    import scala.reflect.macros.Context
    
    object SealedExample {
      def values[A]: Set[A] = macro values_impl[A]
    
      def values_impl[A: c.WeakTypeTag](c: Context) = {
        import c.universe._
    
        val symbol = weakTypeOf[A].typeSymbol
    
        if (!symbol.isClass) c.abort(
          c.enclosingPosition,
          "Can only enumerate values of a sealed trait or class."
        ) else if (!symbol.asClass.isSealed) c.abort(
          c.enclosingPosition,
          "Can only enumerate values of a sealed trait or class."
        ) else {
          val children = symbol.asClass.knownDirectSubclasses.toList
    
          if (!children.forall(_.isModuleClass)) c.abort(
            c.enclosingPosition,
            "All children must be objects."
          ) else c.Expr[Set[A]] {
            def sourceModuleRef(sym: Symbol) = Ident(
              sym.asInstanceOf[
                scala.reflect.internal.Symbols#Symbol
              ].sourceModule.asInstanceOf[Symbol]
            )
    
            Apply(
              Select(
                reify(Set).tree,
                newTermName("apply")
              ),
              children.map(sourceModuleRef(_))
            )
          }
        }
      }
    }
    

    Now we can write the following:

    scala> val keys: Set[ResizedImageKey] = SealedExample.values[ResizedImageKey]
    keys: Set[ResizedImageKey] = Set(Large, Medium, Small)
    

    And this is all perfectly safe—you’ll get a compile-time error if you ask for values of a type that isn’t sealed, has non-object children, etc.

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

Sidebar

Related Questions

I just wanted to know if it was possible to do something like: CREATE
Just wanted to know if what I'm trying to do is possible since I
I just wanted to know if this is possible. i Have a Table like
Just wanted to know if is possible to use mod_rewrite on a single(or more
Just wanted to know. Is it possible to highlight text in ElasticSearch on an
I just wanted to know if it is 100% possible, if my language is
i just wanted to know if it is possible to check in mouse event
Just wanted to know something about signed vs unsigned interpretation. Am I right if
I just wanted to know whether it is possible to pass geocoords to the
I'm almost sure this is not possible, but just wanted to ask if I'm

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.