How to add a method to a Scala Enumeration object?

I use Scala 2.8 and define the enumeration as follows:

object Stooges extends Enumeration { type Stooge = Value val Larry, Curly, Moe = Value } 

And I want to add a method to this enumeration, which cycles to the "next" puppet. I can define such a method outside of Stooges, and it works very well in a test program:

 def nextStooge(v:Stooges.Stooge):Stooges.Stooge = Stooges((v.id+1) % Stooges.values.size) 

I would really like to add this method to Stooges, and I tried to do something (using Stooges.Stooge and Stooges.Value). Both are compiled. But run this program:

 import Stooges._ object App extends Application { println(Stooges.nextStooge(Larry)) } 

gives:

 Exception in thread "main" java.lang.ExceptionInInitializerError at demo.App.main(Stooges.scala) Caused by: java.lang.IllegalArgumentException: wrong number of arguments at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25) at java.lang.reflect.Method.invoke(Method.java:592) at scala.Enumeration$$anonfun$scala$Enumeration$$nameOf$2.apply(Enumeration.scala:176) at scala.Enumeration$$anonfun$scala$Enumeration$$nameOf$2.apply(Enumeration.scala:171) at scala.collection.TraversableLike$WithFilter$$anonfun$foreach$1.apply(TraversableLike.scala:1200) at scala.collection.IndexedSeqLike$class.foreach(IndexedSeqLike.scala:85) at scala.collection.mutable.ArrayOps.foreach(ArrayOps.scala:20) at scala.collection.TraversableLike$WithFilter.foreach(TraversableLike.scala:1199) at scala.Enumeration.scala$Enumeration$$nameOf(Enumeration.scala:171) at scala.Enumeration$Val.toString(Enumeration.scala:237) at java.lang.String.valueOf(String.java:2615) at java.io.PrintStream.print(PrintStream.java:616) at java.io.PrintStream.println(PrintStream.java:753) at scala.Console$.println(Console.scala:198) at scala.Predef$.println(Predef.scala:152) at demo.App$.<init>(Stooges.scala:14) at demo.App$.<clinit>(Stooges.scala) 

Is this a mistake or just a bad idea?

+3
source share
2 answers

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 much better to say Larry.nextStooge instead of nextStooge(Larry) . I assume that you should implement this with a special private class.

+4
source

This is mistake. This initialization does something terribly strange. I am going to open a ticket (done, # 2827 ) if it does not exist.

+2
source

Source: https://habr.com/ru/post/1302886/


All Articles