smeeb smeeb - 1 month ago 7
Groovy Question

Groovy enum constructor matching

I have the following Groovy enum:

enum Flower {
Tulip(1),
Daisy(2)

private int rank

Flower(int rank) {
super()
this.rank = rank
}

int getRank() {
rank
}
}


I also have a
GardernGenerator
class that takes a
Flower
instance as one of its constructor parameters:

class GardenGenerator {
Flower flower
int length
int width
boolean isOrganic

GardenGenerator(Flower flower, int length, int width, boolean isOrganic) {
super()

this.flower = flower
this.length = length
this.width = width
this.isOrganic =isOrganic
}

Garden createGarden() {
// ...blah whatever
}
}


When I try to create this
GardenGenerator
instance like so:

GardenGenerator gg = new GardenGenerator(Flower.Daisy, 5, 10, true)


I get constructor-related issues on the
Flower
enum
:

Caused by: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: com.me.myapp.Flower(java.lang.String, java.lang.Integer, java.lang.Integer)


What do I need to do to
Flower
to fix this?

Answer Source

Your Flower enum should be implemented as follows:

enum Flower {
    Tulip(1),
    Daisy(2)

    private int rank

    Flower(int rank) {
        this.rank = rank
    }

    int getRank() {
        rank
    }
}

There's no need for super() call in enum's constructor.