Genevieve
Genevieve

Reputation: 975

Create a function in Kotlin that takes an enum parameter

I'm creating a function that takes an enum value as a parameter, but I am very new to Kotlin and I can't find any material that covers this specifically.

Example:

enum class Color(val rgb: Int) {
        RED(0xFF0000),
        ORANGE(0xffa500),
        YELLOW(0xffff00),
        GREEN(0x00FF00),
        BLUE(0x0000FF),
        INDIGO(0x4b0082),
        VIOLET(0x8F5E99)
}
fun getHexColor (Color: Enum)
{
    when(x){
        Color.BLUE -> println("Battle")
        else -> print("otherwise")
    }

}

I get an error that says:

One type argument expected for class Enum<E: Enum<E>>

I've looked through Kotlin documentation for over an hour and I've got nothing to show for it... do any of you have an idea of how to use this class as a parameter?

Upvotes: 2

Views: 5983

Answers (6)

Siru malayil
Siru malayil

Reputation: 355

 /**
 * Enum representing App Events.
 * Each event has a corresponding event name.
 */
enum class AppEvents(eventName: String) {
   
    SIGN_UP("Sign up"),
    TRANSACTION_SUCCESS("Transaction success"),
    REPEAT_TRANSACTION("Repeat transaction"),
    TRANSACTION_FAILURE("Transaction failure"),
    LOGIN("Login");

    /**
     * Returns the event name associated with the enum constant.
     */
    var eventName: String? = eventName
}

// Usage Example

val signUpEventName = AppEvents.SIGN_UP.eventName

In this Kotlin enum class AppEvents, each enum constant is associated with an event name. The constructor parameter eventName is used to initialize the event name for each enum constant. The eventName function returns the event name associated with the enum constant.

In the usage example, we retrieve the event names for different events by calling the eventName function on the enum constants. If an event name is nullable, we use the Elvis operator (?:) to provide a default value.

Upvotes: 0

aleksandrbel
aleksandrbel

Reputation: 1490

You are able to do it like this with an interface for example:

enum class Color(val rgb: Int): IHexColor {

    RED(0xFF0000){
        override fun getHexColor() = rgb.toString()
    },
    GREEN(0x00FF00){
        override fun getHexColor(): String = rgb.toString()
    },
    BLUE(0x0000FF){
        override fun getHexColor(): String = rgb.toString()
    }
}

interface IHexColor {
    fun getHexColor(): String
}

@Test
fun testBasic() {
    val red = Color.RED
    val green = Color.GREEN
    val blue = Color.BLUE

    val palette = arrayListOf(red, green, blue)

    palette.forEach {
        println("Color: $it :: hex - ${it.getHexColor()}")
    }
}

// output => Color: RED :: hex - 16711680, Color: GREEN :: hex - 65280, Color: BLUE :: hex - 255

How to use enum class:

fun useColorClass(color: Color){
    println(color.rgb)
}

@Test
fun testColor() {
    useColorClass(Color.RED)
    useColorClass(Color.BLUE)
    useColorClass(Color.GREEN)
}

// output => 16711680, 255, 65280

An answer for your question:

fun getHexColor (c: Color): String {
   return when(x){
        Color.BLUE -> println("Battle")
        else -> print("otherwise")
    }
}

Upvotes: 0

Willi Mentzel
Willi Mentzel

Reputation: 29844

You have to use the type which is Color:

fun getHexColor (x: Color) {
    when(x){
        Color.BLUE -> println("Battle")
        else -> print("otherwise")
    }
}

Note that a function prefixed with "get" should return something. Since when is an expression you can do it like this:

fun getHexColor (x: Color) = when(x) { // will return a String
        Color.BLUE -> "Battle"
        else -> "otherwise"
}

println(getHexColor(Color.BLUE))

Upvotes: 1

s1m0nw1
s1m0nw1

Reputation: 81879

A function syntax in Kotlin looks like this:

fun double(x: Int): Int {
    return 2 * x
}

where x is the name of the function parameter of type Int. Your function is not valid since you use Color as the parameter name rather than its type. To fix it do:

fun getHexColor (color: Color) {
    when(color){
        Color.BLUE -> println("Battle")
        else -> print("otherwise")
    }
}

Upvotes: 0

Saurabh Patel
Saurabh Patel

Reputation: 175

enum creates a new class so you can use it as function argument type, as shown below. For functions in kotlin see here.

fun getHexColor (x : Color)
{
    when(x){
        Color.BLUE -> println("Battle")
        else -> print("otherwise")
    }

}

Upvotes: 1

Cililing
Cililing

Reputation: 4753

Enum is actually special kind of class (it is even called enum class). So, use it as normal class and use benefits you get from it.

Example:

enum class X {
    X, Y
}

fun check(param: X) {
    val unit = when (param) {
        X.X -> println("x")
        X.Y -> println('y')
    }
}

Upvotes: 0

Related Questions