7

Say we have enums

enum class Status {
    OPEN, CLOSED
}

enum class Weekday {
    WORKDAY, DAYOFF
}

Having a Java class

public KotlinInvoker {
    public methodWithKotlinEnumAsParameter_namely_AppendWorkingStatusString( ? kotlinEnum) {
    ...
    }
}

The Goal is to directely pass ANY jave / kotlin enum to that kind of the function like if Java you would have a

    <E extends java.lang.Enum<E>>
    methodAcceptingEnumAsParameter(E enum) {
    ...
    return result + ' ' + enum.toString();
    }

so you can pass ANY enum to it. what should be the method signature to play nicely with kotlin enum as well as it is mapped to java enum accordingly to official kotlin docs?

SoBeRich
  • 682
  • 2
  • 8
  • 15
  • Any luck on this? I have a java class that takes an enum in its constructor. Problem is my enum class is written in kotlin.. I could convert the class w/ constructor into kotlin but I'm looking for a solution that allows me to keep the class in java and pass in a kotlin enum. – Kyle Apr 04 '19 at 14:20
  • @Kyle no solution. Those are simply different class declarations / types in contrast with most of the types been mapped by kotlin stdlib. No "clever" way. Unfortunately. – SoBeRich Apr 04 '19 at 19:10

1 Answers1

10

Your Java example works in Kotlin just fine:

enum class Status {
    OPEN, CLOSED
}

enum class Weekday {
    WORKDAY, DAYOFF
}

fun <E : Enum<E>> methodWithKotlinEnumAsParameter(arg : E)
{
    println(arg.name)
}

Now, if you for example call methodWithKotlinEnumAsParameter(Weekday.DAYOFF), it will print DAYOFF to the console.

msrd0
  • 7,816
  • 9
  • 47
  • 82
  • The header says "How to pass ..Kotlin enum as parameter to a method **IN JAVA CODE**? You method is in Kotlin, So, thank you for attempt, please re-read the question. – SoBeRich Jun 04 '18 at 00:18