Is hashCode of a scala enum is the same on different JVM (spark) ?

Viewed 102

I read that a good practice for enum is scala is as follow.

I deliberately extends the class with Serializable for Spark.

sealed abstract class MyEnum(val nature: String) extends Serializable
case object A extends MyEnum("a")
case object B extends MyEnum("b")

The problem is that i want that the enum should be extensible by others, thus i have to remove the sealed keyword to enable this functionality

abstract class MyEnum(val nature: String) extends Serializable
case object A extends MyEnum("a")
case object B extends MyEnum("b")

On another file

import enumpackage.MyEnum
case object C extends MyEnum("c")

Knowing this issue with Java enum on Spark, i was wondering how is generate the hashCode from object that extends class that are sealed or not.

Is it safe to avoid sealed keyword for my purpose or do i have to keep it, why ? If i have to keep it, is there any solution to my extensibility problem.

1 Answers

In programming languages, particularly in Java & Scala enumerations are not extendible by the user, but only by the owner of the enumeration and usually for good reason, control. If you are getting rid of sealed then don't consider it an enumeration, just consider it just like any other class with subclasses or subobjects.

As for your hashCode. You get that automatically with case class or case object, so there is nothing extra you really need to do.

Related