-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Enum initializers from rawValue #94
Comments
For reference, there is also a |
Wait, we should try this approach:
Because, apparently that works as well, and allows to keep the same caller code:
|
Great!
Strange way that constructor is made :-)
… On 18 Feb 2019, at 12:32, Angel G. Olloqui ***@***.***> wrote:
Wait, we should try this approach:
enum class PaymentMethodType constructor(var rawValue: String) {
direct("DIRECT"), creditCard("CREDIT_CARD");
companion object {
operator fun invoke(rawValue: String) = PaymentMethodType.values().firstOrNull { it.rawValue == rawValue }
}
}
Because, apparently that works as well, and allows to keep the same caller code:
val method = PaymentMethodType(rawValue = "DIRECT")
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub <#94 (comment)>, or mute the thread <https://github.com/notifications/unsubscribe-auth/AB-irqr8oc8puseI2co4I5oYAW25TtaIks5vOo8ygaJpZM4bAoMm>.
|
Do you want me to add this in a new branch, or will you do it? |
I think you already got some stuff working for this, didn't you? if so, please go ahead. Otherwise, I will try to find some time, but as usual, I can not promise on dates. |
Merged with #97 |
In Swift, when you have an enum like:
The you can instantiate the enum from a rawValue like:
let type = PaymentMethodType(rawValue: "DIRECT")
In Kotlin, the equivalent would be:
But that does not provide the Swift constructor from
rawValue
.An option, would be to create an additional method on the Kotlin code like:
But this also affects the way the caller needs to invoke the instantiation to:
val type = PaymentMethodType.from(rawValue = "DIRECT")
The text was updated successfully, but these errors were encountered: