OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Is there any way to override the name of enum in scala?

  • Thread starter Thread starter sagar.musale
  • Start date Start date
S

sagar.musale

Guest
I am new to the Scala. I am sorry if it's just basic one.

I want to have a way where I can Override the enum name using any method.

Code:
object ErrorCode extends Enumeration {

  type ErrorCode = Value
  
  val UnexpectedError = Value(1, "Unexpected Error")
  val ParamError = Value(2, "Param Error")

  val conflictingValues = Set(
    UnexpectedError
  )

  def get(id: Int): Option[ErrorCode.Value] =
    ErrorCode.values.find(x => x.id == id)

  // Some way to override
  def getErrorWithDifferentName(id: Int): Option[ErrorCode.Value] = {
  .... // some code which can return `Value(1, "Invalid Error")`
  }
}

I tried to create a Value at runtime in a enum function but didn't work.

Code:
  def getErrorWithDifferentName(id: Int): Option[ErrorCode.Value] = {
    if (conflictingValues.exists(x => x.id == id)) {
      Some(Value(id, getNameForConflictingError(id)))
    } else {
      get(id)
    }
  }

  private def getNameForConflictingError(id: Int): String = {
    "Invalid Error"
  }

Expectations

Code:
val err = ErrorCode(1).toString // Unexpected Error
val newErr = ErrorCode.getErrorWithDifferentName(1).toString // Invalid Error
<p>I am new to the Scala. I am sorry if it's just basic one.</p>
<p>I want to have a way where I can Override the enum name using any method.</p>
<pre><code>object ErrorCode extends Enumeration {

type ErrorCode = Value

val UnexpectedError = Value(1, "Unexpected Error")
val ParamError = Value(2, "Param Error")

val conflictingValues = Set(
UnexpectedError
)

def get(id: Int): Option[ErrorCode.Value] =
ErrorCode.values.find(x => x.id == id)

// Some way to override
def getErrorWithDifferentName(id: Int): Option[ErrorCode.Value] = {
.... // some code which can return `Value(1, "Invalid Error")`
}
}
</code></pre>
<p>I tried to create a <code>Value</code> at runtime in a enum function but didn't work.</p>
<pre><code> def getErrorWithDifferentName(id: Int): Option[ErrorCode.Value] = {
if (conflictingValues.exists(x => x.id == id)) {
Some(Value(id, getNameForConflictingError(id)))
} else {
get(id)
}
}

private def getNameForConflictingError(id: Int): String = {
"Invalid Error"
}
</code></pre>
<p>Expectations</p>
<pre><code>val err = ErrorCode(1).toString // Unexpected Error
val newErr = ErrorCode.getErrorWithDifferentName(1).toString // Invalid Error
</code></pre>
I have found how to fix this.
 

Latest posts

Online statistics

Members online
0
Guests online
4
Total visitors
4
Top