How to add a method to a Scala Enumeration object?

This works for me: scala> object Stooges extends Enumeration { | type Stooge = Value | val Larry = Value("Larry") | val Curly = Value("Curly") | val Moe = Value("Moe") | | def nextStooge(v:Stooges. Stooge) = Stooges((v. Id+1) % Stooges.

MaxId) | } defined module Stooges scala> scala> import Stooges. _ import Stooges. _ scala> nextStooge(Larry) res0: Stooges.

Value = Curly scala> nextStooge(Curly) res1: Stooges. Value = Moe scala> nextStooge(Moe) res2: Stooges. Value = Larry It would be definetively nicer to be able to say Larry.

NextStooge instead of nextStooge(Larry) I suppose you have to implement that with a custom sealed class.

This works for me: scala> object Stooges extends Enumeration { | type Stooge = Value | val Larry = Value("Larry") | val Curly = Value("Curly") | val Moe = Value("Moe") | | def nextStooge(v:Stooges. Stooge) = Stooges((v. Id+1) % Stooges.

MaxId) | } defined module Stooges scala> scala> import Stooges. _ import Stooges. _ scala> nextStooge(Larry) res0: Stooges.

Value = Curly scala> nextStooge(Curly) res1: Stooges. Value = Moe scala> nextStooge(Moe) res2: Stooges. Value = Larry It would be definetively nicer to be able to say Larry.

NextStooge instead of nextStooge(Larry). I suppose you have to implement that with a custom sealed class.

Indeed, not needing to pass the arg to nextStooge (and just calling it next()) was my original goal. Interesting that this works when specifying each val separately and naming it. I'll play some more.

– Alex Miller Dec 21 '09 at 14:56.

This is a bug. That initalization is doing something awfully strange. I'm going to open a ticket (done, #2827), if none exists.

Many thanks.... – Alex Miller Dec 21 '09 at 14:55.

And I want to add a method to this Enumeration that cycles to the "next" stooge. What I'd really like is to add this method to Stooges and I have tried doing such a thing (using both Stooges. Stooge and Stooges.

Those both compile.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions