Marco
Marco

Reputation: 15929

Groovy constructor giving compile error 'due to hash collision" in constructors

I am trying to add a constructor to a Groovy class, but adding a second method as a constructor i am getting a compile error..

Unable to compile class ... due to hash collision in constructors

class BaseException extends RuntimeException {

private Integer status
private String message
private Long timestap
private List<ErrorMessage> errors

    BaseException(Integer status, String message, List<ErrorMessage> errorMessageList) {
        this.status = status
        this.message = message
        this.timestap = System.currentTimeMillis();
        this.errors = errorMessageList
    }

    // ---
    // adding the method below gives a compile error
    // ---
    BaseException(Integer status, String message, List<ErrorCode> errorCodeList) {
        this.status = status
        this.message = message
        this.timestap = System.currentTimeMillis();
        this.errors = []
        errorCodeList.each { error ->
            this.errors.add(new ErrorMessage(error.code, error.description))
        }
    }
    .. code emitted
}

Any hint what i am doing wrong?

Upvotes: 0

Views: 411

Answers (1)

tim_yates
tim_yates

Reputation: 171144

You can't have two constructors or methods with the same signature once generics are removed due to erasure.

Same goes for Java.

If you really really need this, it's usual to have two static factory method with different names and a less accessible constructor that handles both cases

Upvotes: 1

Related Questions