Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pass Scala enums as parameters?

Tags:

My Enumeration is as follows

object Market extends Enumeration {    type Market = Value   val ASX = Value("ASX")   val LSE = Value("LSE")  } 

then

class MyClass (currentMarket: Market) { } 

Results in the following:

not found: type Market

like image 437
deltanovember Avatar asked Jul 16 '11 13:07

deltanovember


People also ask

Can enum be parameterized?

We have to create parameterized constructor for this enum class. Why? Because as we know that enum class's object can't be create explicitly so for initializing we use parameterized constructor. And the constructor cannot be the public or protected it must have private or default modifiers.

Is there enum in Scala?

In Scala, there is no enum keyword unlike Java or C. Scala provides an Enumeration class which we can extend in order to create our enumerations. Every Enumeration constant represents an object of type Enumeration. Enumeration values are defined as val members of the evaluation.

Can we pass an enum value as parameter and return enum value?

enums are technically descendants of Enum class. So, if you want to use only Enum's standard methods in your method (such as values()), pass the parameter like this: static void printEnumValue(Enum generalInformation) See, that the only thing you have to change is the capital letter E.


1 Answers

You have to import the enumeration:

import Market._ 

It's more common, though, to just write Market.Value to refer to the enumeration type. That also saves you the type alias in Market.

like image 102
Martin Odersky Avatar answered Oct 01 '22 00:10

Martin Odersky