Home > OS >  what is the type of variables defined in enum?
what is the type of variables defined in enum?

Time:07-21

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.

  1. Is AllEnums is a object of class MyEnums.scala ?

  2. If 1 point is correct that means, MyEnums.scala extending class MyEnums.scala ?

  3. 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 ?

  4. val one,two,three = Value, In this statment Is = Value means the values assigned to val one,two,three are of type Value ?

  5. val one,two,three, Are these instance variables of class MyEnums.scala, whic is nothing but of Enumeration Type ?

  6. what's a type of these vals one,two,three ? Assigning values to these are of Value type, but is there any type of these variables itself one,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.

  • Related