object weekdays extends Enumeration(1, "Monday", "Tue...?

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

object weekdays extends Enumeration(1, "Monday", "Tue...?

Rob Dickens-3
Dear scalaUser: List,

Please could somebody help me out by explaining why the resulting enumeration doesn't  behave as I'd hoped:

object weekdays extends
  Enumeration(1, "Monday", "Tuesday", "Wednesday",
  "Thursday", "Friday", "Saturday", "Sunday") {}
println(weekdays)
println(weekdays(1)}

this produces

{}
java.util.NoSuchElementException: key not found: 1

Thanks,

Rob
Reply | Threaded
Open this post in threaded view
|

Re: object weekdays extends Enumeration(1, "Monday", "Tue...?

James Iry-2
You need values as part of your Enumeration.

object weekdays extends Enumeration(1, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") {
   val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = Value
}


scala> weekdays(1)
res5: weekdays.Value = Monday


On Sun, Jul 20, 2008 at 12:07 PM, Rob Dickens <[hidden email]> wrote:
Dear scalaUser: List,

Please could somebody help me out by explaining why the resulting enumeration doesn't  behave as I'd hoped:

object weekdays extends
  Enumeration(1, "Monday", "Tuesday", "Wednesday",
  "Thursday", "Friday", "Saturday", "Sunday") {}
println(weekdays)
println(weekdays(1)}

this produces

{}
java.util.NoSuchElementException: key not found: 1

Thanks,

Rob

Reply | Threaded
Open this post in threaded view
|

Re: object weekdays extends Enumeration(1, "Monday", "Tue...?

Rob Dickens-3
I suppose I was expecting more for my money (as it were). Thinking more about it, having to enumerate the vals yourself does now make sense.

Thanks for responding,

Rob

On Sun, Jul 20, 2008 at 9:30 PM, James Iry <[hidden email]> wrote:
You need values as part of your Enumeration.


object weekdays extends Enumeration(1, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") {
   val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = Value
}


scala> weekdays(1)
res5: weekdays.Value = Monday



On Sun, Jul 20, 2008 at 12:07 PM, Rob Dickens <[hidden email]> wrote:
Dear scalaUser: List,

Please could somebody help me out by explaining why the resulting enumeration doesn't  behave as I'd hoped:

object weekdays extends
  Enumeration(1, "Monday", "Tuesday", "Wednesday",
  "Thursday", "Friday", "Saturday", "Sunday") {}
println(weekdays)
println(weekdays(1)}

this produces

{}
java.util.NoSuchElementException: key not found: 1

Thanks,

Rob


Reply | Threaded
Open this post in threaded view
|

Re: object weekdays extends Enumeration(1, "Monday", "Tue...?

James Iry-2
Yeah.  Unlike Java Enumerations, Scala's Enumeration doesn't have any language support.  It's pure library.  That limits the amount of "auto-magic."

On Mon, Jul 21, 2008 at 9:15 AM, Rob Dickens <[hidden email]> wrote:
I suppose I was expecting more for my money (as it were). Thinking more about it, having to enumerate the vals yourself does now make sense.

Thanks for responding,

Rob

On Sun, Jul 20, 2008 at 9:30 PM, James Iry <[hidden email]> wrote:
You need values as part of your Enumeration.


object weekdays extends Enumeration(1, "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday") {
   val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = Value
}


scala> weekdays(1)
res5: weekdays.Value = Monday



On Sun, Jul 20, 2008 at 12:07 PM, Rob Dickens <[hidden email]> wrote:
Dear scalaUser: List,

Please could somebody help me out by explaining why the resulting enumeration doesn't  behave as I'd hoped:

object weekdays extends
  Enumeration(1, "Monday", "Tuesday", "Wednesday",
  "Thursday", "Friday", "Saturday", "Sunday") {}
println(weekdays)
println(weekdays(1)}

this produces

{}
java.util.NoSuchElementException: key not found: 1

Thanks,

Rob



Reply | Threaded
Open this post in threaded view
|

Re: object weekdays extends Enumeration(1, "Monday", "Tue...?

Aaron Harnly-2-2
Hi guys,

On Sun, Jul 20, 2008 at 12:07 PM, Rob Dickens wrote:
> object weekdays extends Enumeration(1,
> "Monday", "Tuesday", "Wednesday", "Thursday","Friday", "Saturday", "Sunday"
> )

James Iry <jamesiry@...> writes:
> Yeah. Unlike Java Enumerations, Scala's Enumeration doesn't have any
> language support. It's pure library. That limits the amount of
> "auto-magic."

FYI: I was annoyed by the Enumeration("Monday","Tuesday") { val Monday,
Tuesday ... } repetition.

So noodling around one day I cooked up a variant of Enumeration that
uses the following silly trick, in which the values' string
representation is inferred from their name:

object weekdays extends NamedEnum {
  lazy val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = Value
  init
}

scala> weekdays("Monday")
res1: weekdays.Value = Monday

scala> weekdays.Wednesday
res3: weekdays.Value = Wednesday

Using lazy vals is required for the trick to work; unfortunate side
effects are (a) the initialization requirement ; (b) that this approach
doesn't permit an ordering. Twinkie to anyone who can figure out a way
to fix either one...

Code is below:

class NamedEnum {
        def apply(name: String): Value = nameMap(name)
        def values: Iterable[Value] = nameMap.values.toList
        private var nameMap =  Map.empty[String,Value]

        protected def Value: Value = {
                val newName = try {
                        throw new Exception()
                } catch {
                        case e: Exception => e.getStackTrace()(1).getMethodName
                }
                val newValue = new Value(newName)
                nameMap = nameMap + (newName -> newValue)
                newValue
        }
       
        protected def valueMethods = getClass.getMethods.toList.filter( m =>
                (m.getReturnType == classOf[Value]) &&
                (m.getParameterTypes.length == 0) &&
                (m.getDeclaringClass == getClass)
        )
       
        // We have to invoke each method once to make sure the values
        // get loaded into the name map.
        def init {
                valueMethods.foreach( _.invoke(this,null) )
        }
       
        class Value(val name: String) {
                override def toString = name
        }
       
        lazy val name = this.getClass.getName.replaceAll("\\$.*$","")
}



Reply | Threaded
Open this post in threaded view
|

Re: object weekdays extends Enumeration(1, "Monday", "Tue...?

Landei

Aaron Harnly-2 wrote
Hi guys,

FYI: I was annoyed by the Enumeration("Monday","Tuesday") { val Monday,
Tuesday ... } repetition.

So noodling around one day I cooked up a variant of Enumeration that
uses the following silly trick, in which the values' string
representation is inferred from their name:

object weekdays extends NamedEnum {
  lazy val Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday = Value
  init
}

scala> weekdays("Monday")
res1: weekdays.Value = Monday

scala> weekdays.Wednesday
res3: weekdays.Value = Wednesday

Using lazy vals is required for the trick to work; unfortunate side
effects are (a) the initialization requirement ; (b) that this approach
doesn't permit an ordering. Twinkie to anyone who can figure out a way
to fix either one...

Code is below:

class NamedEnum {
        def apply(name: String): Value = nameMap(name)
        def values: Iterable[Value] = nameMap.values.toList
        private var nameMap =  Map.empty[String,Value]

        protected def Value: Value = {
                val newName = try {
                        throw new Exception()
                } catch {
                        case e: Exception => e.getStackTrace()(1).getMethodName
                }
                val newValue = new Value(newName)
                nameMap = nameMap + (newName -> newValue)
                newValue
        }
       
        protected def valueMethods = getClass.getMethods.toList.filter( m =>
                (m.getReturnType == classOf[Value]) &&
                (m.getParameterTypes.length == 0) &&
                (m.getDeclaringClass == getClass)
        )
       
        // We have to invoke each method once to make sure the values
        // get loaded into the name map.
        def init {
                valueMethods.foreach( _.invoke(this,null) )
        }
       
        class Value(val name: String) {
                override def toString = name
        }
       
        lazy val name = this.getClass.getName.replaceAll("\\$.*$","")
}
Interesting! I played around a little bit, but couldn't solve the problems. One little remark: You don't have to throw an Exception in order to get the stack trace, you can simply write:

val newName = new Throwable().getStackTrace()(1).getMethodName

Take care!
Landei