Skip to content
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

Support Enum Companion #41

Open
atollk opened this issue Aug 27, 2023 · 3 comments
Open

Support Enum Companion #41

atollk opened this issue Aug 27, 2023 · 3 comments
Labels
wontfix This will not be worked on

Comments

@atollk
Copy link

atollk commented Aug 27, 2023

@KustomExport
enum class E {
    EnumValue;

    companion object {
        val static = 1
    }
}

generates

@JsExport
public class E internal constructor(
    internal val common: CommonE,
) {
    public val name: String = common.name
}

@JsExport
public fun E_values(): Array<E> = arrayOf(E_EnumValue, E_Companion)

@JsExport
public fun E_valueOf(name: String): E? {
    if (name == E_EnumValue.name)
        return E_EnumValue

    if (name == E_Companion.name)
        return E_Companion

    return null
}

public fun E.importE(): CommonE = common

public fun CommonE.exportE(): E = E_valueOf(this.name)!!

@JsExport
public val E_EnumValue: E = E(CommonE.EnumValue)

@JsExport
public val E_Companion: E = E(CommonE.Companion)

which is an error because Type mismatch: inferred type is E.Companion but E was expected in the last line. The incorrect assumption is made that the companion object is an enum value.

Workaround: Use JsExport instead of KustomExport

@glureau
Copy link
Collaborator

glureau commented Aug 29, 2023

I don't think this will be supported: KustomExport goal was to provide enum support in older Kotlin version, now that newest Kotlin supports it, it's more than advised to use JsExport (unfortunately there's no doc saying that today). Using KustomExport on an enum is only adding more code to your js bundle, passthrough code so possible performance impact and the exposed syntax is not the Kotlin one.

I may remove the enum support completely in a future version.

@glureau glureau added the wontfix This will not be worked on label Aug 29, 2023
@atollk
Copy link
Author

atollk commented Aug 29, 2023

That's a fair point but wouldn't KustomExport still provide better functionality in some cases? For example if you want to have a property of type List in the enum.

@glureau
Copy link
Collaborator

glureau commented Aug 30, 2023

It could but I believe some simple workarounds can be found, it's a matter of trade-off so some may need it. As I don't use this tool myself anymore I won't be able to spend time on it on a foreseeable future, feel free to open a PR if you want this feature though.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants