How to access an enum class from another file (which is also a class)?

This is my code for the enum class:

package me.core

enum class RateLimitPrecision(val value: Int) {
    Seconds(0),

    Milliseconds(1);

    companion object {
        fun from(findValue: Int): RateLimitPrecision = RateLimitPrecision.values().first
        {
            it.value == findValue
        }
    }
}

And I want to access this by assigning a variable:

var RateLimitPrecision = RateLimitRecision.from(1);

However this does not seem to work:

image

What have I done wrong, or is it a bug? (I am using IntelliJ Ultimate)

Looks like a typo: RateLimitRecision. Also don’t name your variable the same as your type.

Try:

var precision = RateLimitPrecision.from(1)

Changed it, doesn’t seem to work as well.

image

You just only changed 1 typo to another one :slight_smile:

val precision = RateLimitPrecision.from(1) // and you typed `RateLimitP{r}ecision`

Also fixed
image

Have you checked for errors in your enum class file? There should be one, because you put { on a new line after first.

Is that not allowed?

In this case (passing a lambda as the last or rather only argument), no.

Okay, fixed, and I am still quite confused after I fixed that, it still shows ERROR when I move my pointer to the variable

Please check if there are other errors your IDE or the compiler complains about. Then post a complete example of your code.

So now I cannot even run or debug my script due to this

Could not open cp_init remapped class cache for e4jhdo1d3mdtm54dhxweaf5ox (C:\Users\user\.gradle\caches\6.1\scripts-remapped\wrapper_init_303c1e877ajrdvp1ytr0bf7m8\e4jhdo1d3mdtm54dhxweaf5ox\cp_init4919d77a9a41249da544bc559de1b4ce).
> Could not open cp_init generic class cache for initialization script 'C:\Users\user\AppData\Local\Temp\wrapper_init.gradle' (C:\Users\user\.gradle\caches\6.1\scripts\e4jhdo1d3mdtm54dhxweaf5ox\cp_init\cp_init4919d77a9a41249da544bc559de1b4ce).
   > Could not initialize class org.codehaus.groovy.vmplugin.v7.Java7

I am using JDK 14

You shouldn’t have been able to run or debug it in the first place, because of the brace error. I don’t know if that error message is related to the initial problem.
Please post the current code of your enum class and the .from(1) call.

I mean I cannot even run or build it at the first place due to the error, even though I have the brace error

Anyway, this is my Enum Class

package discordkt.core

enum class RateLimitPrecision(val value: Int) {
    Seconds(0),

    Milliseconds(1);

    companion object {
        fun from(findValue: Int): RateLimitPrecision = RateLimitPrecision.values().first{ it.value == findValue }
    }
}

And this is the call:

var RatelimitPrecision = RateLimitPrecision.from(1);

That code works, so your initial problem is solved (the problems were typos and the brace on the new line). :+1:

Some further suggestions:

  • You are allowed to add a new line after the opening brace. This improves readability in complex code.
  • Remove the semicolon
  • Give your variable a lower-case name (maybe even use val).

I suggest searching the web for the other error and then opening another topic for it, if you can’t solve it.

I searched it up and it seems like the problem is the JDK itself