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

Cannot initialize enum value with static final Java field #346

Open
REASY opened this issue Jul 14, 2022 · 3 comments
Open

Cannot initialize enum value with static final Java field #346

REASY opened this issue Jul 14, 2022 · 3 comments

Comments

@REASY
Copy link

REASY commented Jul 14, 2022

I would want to wrap constants from Java to provide more context not just pure numbers. Imagine I have the following in Java:

package schema;

public final class PointCloudType {
    public static final byte XXX = 0;
    public static final byte PCL_PointWithLabel = 1;
    public static final byte PCL_XYZ = 2;
    public static final String[] names = new String[]{"XXX", "PCL_PointWithLabel", "PCL_XYZ"};

    private PointCloudType() {
    }

    public static String name(int e) {
        return names[e];
    }
}

If I create ByteEnumEntry to wrap them:

package com.motional.platform.map.data.spark.tiler3d.models

import enumeratum.values.{ByteEnum, ByteEnumEntry}

sealed abstract class PointCloudType(val value: Byte) extends ByteEnumEntry

object PointCloudType extends ByteEnum[PointCloudType] {
  val values = findValues

  case object PCL_PointWithLabel extends PointCloudType(value = schema.PointCloudType.PCL_PointWithLabel)

  case object PCL_XYZ extends PointCloudType(value = schema.PointCloudType.PCL_XYZ)
}

I'll get a compilation error:

It looks like not all of the members have a literal/constant 'value:int' declaration, namely: object PCL_PointWithLabel, object PCL_XYZ.

This can happen if:

- The aforementioned members have their `value` supplied by a variable, or otherwise defined as a method

If none of the above apply to your case, it's likely you have discovered an issue with Enumeratum, so please file an issue :)
         
  val values = findValues

@lloydmeta
Copy link
Owner

So, the findValues for value enums require that their values be supplied as a literal, and not a variable or a reference, which is what value = schema.PointCloudType.PCL_PointWithLabel is doing.

@REASY
Copy link
Author

REASY commented Jul 14, 2022

@lloydmeta thanks for the response.

Do you think new behavior can be added? Or it will complicate enumeratum tying to resolve reference?

@lloydmeta
Copy link
Owner

Hmm, it might be possible? I haven't given it a stab, but you're welcome to give it a try!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants