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

  • Home
  • SEARCH
  • 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 8095015
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:59:04+00:00 2026-06-05T20:59:04+00:00

I have to say I don’t understand Scala enumeration classes. I can copy-paste the

  • 0

I have to say I don’t understand Scala enumeration classes. I can copy-paste the example from documentation, but I have no idea what is going on.

object WeekDay extends Enumeration {
  type WeekDay = Value
  val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
}
import WeekDay._
  • What means type WeekDay = Value and why do I have to write that?
  • Why is val Mon = Value? What does that even mean?
  • Why do I have to import the WeekDay
    object? And,
  • when I write val day = WeekDay.Mon, why is it type WeekDay.Value, not type WeekDay?
  • 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-05T20:59:06+00:00Added an answer on June 5, 2026 at 8:59 pm

    the Enumeration trait has a type member Value representing the individual elements of the enumeration (it’s actually an inner class, but the difference doesn’t matter here).

    Thus object WeekDay inherits that type member. The line type WeekDay = Value is just a type alias. It is useful, because after you import it elsewhere with import WeekDay._, you can use that type, e.g.:

    def isWorkingDay(d: WeekDay) = ! (d == Sat || d == Sun)
    

    Instead, a minimal version would just be:

    object WeekDay extends Enumeration {
      val Mon, Tue, Wed, Thu, Fri, Sat, Sun = Value
    }
    

    and you do not have to import the contents of object WeekDay, but then you would need to use type WeekDay.Value and to qualify individual members. So the example would become

    def isWorkingDay(d: WeekDay.Value) = ! (d == WeekDay.Sat || d == WeekDay.Sun)
    

    The second question is about the meaning of val Mon, ... = Value. This is indeed very confusing if you don’t look into the implementation of Enumeration. This is not the assignment of a type! It is instead calling a protected method of the same name, Value, which returns a concrete instance of type Value.

    It so happens that you can write val a, b, c = foo in Scala, and for each value a, b, and c the method foo will be called again and again. Enumeration uses this trick to increment an internal counter so that each value is individual.

    If you open the Scala API docs for Enumeration and click on Visibility: All, you will see that method appearing.

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

Sidebar

Related Questions

Let's say I don't have photoshop, but I want to make pattern files (.pat)
Can I order my users in the database, so I don't have to say
Have to say I really don't understand how come this could happen: I've done
Say I don't have mod_deflate compiled into apache, and I don't feel like recompiling
First off I should say that I don't have any experience in working with
Let's say we have two concave 2d polygons(A, B), that don't intersect. The problem
I have say a dozen types T which inherit from EntityObject and IDataObject .
I have to say the I don't know much about how file formats work.
Let's say I don't have more than one or two dozen objects with different
i have say 20 div's. Is there a way that i can change the

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.