The right way to use Enums in Groovy and Grails

I want to create a series of global enumerations that will be used throughout my application.

I created a groovy file called enums that looks something like this:

class Enums {
    enum GameType{
        Game1,
        Game2,
        Game3
        Game4
        Game5
    }

    enum Enum2{
        Type1,
        Type2,
        Type3
    }

}

The first enumeration works fine, but when I try to use the second, I get the error "inability to resolve the class". What is the correct way to work with Enums in Grails?

+4
source share
2 answers

Each enumeration must have its own class located in src / groovy. I would also suggest using a package for them. Your example should be

SIC / groovy / my / example / GameType.groovy

package my.example
enum GameType{
  Game1,
  Game2,
  Game3,
  Game4,
  Game5
}

SIC / groovy / my / example / Enum2.groovy

package my.example
enum Enum2 {
  Type1,
  Type2,
  Type3
}
+14

; , Enums.groovy.

package com.myapp

enum GameType{
    Game1,
    Game2,
    Game3,
    Game4,
    Game5
}

enum Enum2{
    Type1,
    Type2,
    Type3
}
-1

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


All Articles