I'm new in scala, and while trying to understand below code, found lot of doubts. Please help me to understand this
There is one scala file named as MyEnums.scala
, and code present within it as follow
object AllEnums extends Enumeration {
type Enums = Value
val one,two,three = Value
def getAllValues() : String = values.mkString(",")
}
I'm new in scala, also bit confused same enumeration logic in Java too. Trying to understand how enum defined here.
Is
AllEnums
is a object of classMyEnums.scala
?If
1
point is correct that means,MyEnums.scala
extending classMyEnums.scala
?type Enums = Value
,Value
is type in scala, what's a real significance of it and what this statementtype Enums = Value
is doing in code ?val one,two,three = Value
, In this statment Is= Value
means the values assigned toval one,two,three
are of typeValue
?val one,two,three
, Are these instance variables of classMyEnums.scala
, whic is nothing but ofEnumeration
Type ?what's a type of these
val
sone,two,three
? Assigning values to these are ofValue
type, but is there any type of these variables itselfone,two,three
?
CodePudding user response:
Is AllEnums is a object of class MyEnums.scala ?
No
If 1 point is correct that means, MyEnums.scala extending class MyEnums.scala ?
No
type Enums = Value, Value is type in scala, what's a real significance of it and what this statement type Enums = Value is doing in code ?
type Enums = Value
makes Enums
an alias of Value
.
As for "significance" of it, I am not sure how to answer that. What is the "real significance" of Boolean
?
val one,two,three = Value, In this statment Is = Value means the values assigned to val one,two,three are of type Value ?
Yes
val one,two,three, Are these instance variables of class MyEnums.scala, whic is nothing but of Enumeration Type ?
MyEnums.scala
is a file, not a class.
one, two, three
are members of AllEnums
, and it certainly is not "nothing".
what's a type of these vals one,two,three ?
It is Value
as you said yourself above. Or Enums
, which is the same as. Value
.
Assigning values to these are of Value type, but is there any type of these variables itself one,two,three ?
Yes. Value
.