4

What it's like now:

class Trial {
    companion object{
        @JvmStatic fun main(args: Array<String>){
            val message = Message.ALREADY_REGISTERED.value
            println(message);
        }
    }
}

enum class Message constructor(val value: String){
    ALREADY_REGISTERED("You've already been registered")
}

This outputs: You've already been registered. But I was wondering, is there a way to just assign val message = Message.ALREADY_REGISTERED and have the value immediately without having to use .value?

When trying this (with the code I want)

class Trial {
    companion object{
        @JvmStatic fun main(args: Array<String>){
            val message = Message.ALREADY_REGISTERED
            println(message);
        }
    }
}

enum class Message constructor(val value: String){
    ALREADY_REGISTERED("You've already been registered")
}

it outputs ALREADY_REGISTERED.

So can I make the value of the enum return default instead of it's name?

3
  • The question looks like you are asking for an implicit type conversion Message -> String, while you are probably not. Commented Jan 12, 2017 at 17:15
  • Well that would be a workaround but quite possibly could work Commented Jan 12, 2017 at 17:26
  • constructor is superfluous Commented Jan 12, 2017 at 17:29

1 Answer 1

8

You need to override toString() for your enum class:

enum class Message(val value: String) {
    ALREADY_REGISTERED("You've already been registered");

    override fun toString() = value
}

You can also use class delegation if you want to be able to treat your enum class as a CharSequence (like StringBuilder and other String-like classes):

enum class Message(val value: String) : CharSequence by value {
    ALREADY_REGISTERED("You've already been registered");

    override fun toString() = value
}

Then you can use it just like any other CharSequence. e.g.:

buildString {
    append("Message received: ")
    appendln(Message.ALREADY_REGISTERED)
    appendln()
    appendln("Don't forget to do your laundry.")
}
Sign up to request clarification or add additional context in comments.

6 Comments

thanks, just wondering: why is the semicolon required? Because compiler will think that I'm trying to declare another enum?
This only seems to work when using println, if message is changed to val message: String = Message.ALREADY_REGISTERED the same problem is still present
I've updated my answer. You cannot extend String but you can get close. Java/Kotlin does this in lots of places with the CharSequence interface which String implements. In fact, when ever you define methods that receive a String and you do not require that input String to be immutable then you should use CharSequence instead for this exact purpose (e.g. take a look at the various methods in the kotlin.text package).
Hmm then the .value will be easier haha. Thanks for your help!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.