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

Enum generated as Java interface #81

Closed
crmorello opened this issue Mar 27, 2023 · 6 comments
Closed

Enum generated as Java interface #81

crmorello opened this issue Mar 27, 2023 · 6 comments
Assignees
Labels

Comments

@crmorello
Copy link

Just curious if there is a particular reason why Cito enums are generated as separate Java interfaces?
This:

public enum TestEnum { 
    A, 
    B 
}

Generates a separate TestEnum.java file that contains:

public interface TestEnum
{
	int A = 0;
	int B = 1;
}

I would have expected the following since the syntax is almost identical in this case

public enum TestEnum { 
    A, 
    B;
}
@crmorello crmorello changed the title Enums generated as Java interface Enum generated as Java interface Mar 27, 2023
@pfusik pfusik self-assigned this Mar 29, 2023
@pfusik
Copy link
Collaborator

pfusik commented Mar 29, 2023

Thank you for this question!

This decision was made long time ago, when Android was avoiding Java enums.
It looks like Android's reasons are now obsolete so I'm going to change cito to emit Java enums.

@pfusik
Copy link
Collaborator

pfusik commented Mar 29, 2023

How to transpile flags? I'm aware of EnumSet, but how would you transpile:

public enum* Inventory
{
	None = 0,
	Gun = 1,
	Knife = 2,
	Weapons = Gun | Knife,
	Key = 4
}

?

@pfusik
Copy link
Collaborator

pfusik commented Mar 29, 2023

import java.util.EnumSet;

public enum Inventory
{
	_GUN,
	_KNIFE,
	_KEY;
	public static final EnumSet<Inventory> NONE = EnumSet.noneOf(Inventory.class);
	public static final EnumSet<Inventory> GUN = EnumSet.of(_GUN);
	public static final EnumSet<Inventory> KNIFE = EnumSet.of(_KNIFE);
	public static final EnumSet<Inventory> WEAPONS = EnumSet.of(_GUN, _KNIFE);
	public static final EnumSet<Inventory> KEY = EnumSet.of(_KEY);

	/// The `&` operator.
	public static EnumSet<Inventory> retaining(EnumSet<Inventory> a, EnumSet<Inventory> b)
	{
		EnumSet<Inventory> result = EnumSet.copyOf(a);
		result.retainAll(b);
		return result;
	}

	/// The `|` operator.
	public static EnumSet<Inventory> adding(EnumSet<Inventory> a, EnumSet<Inventory> b)
	{
		EnumSet<Inventory> result = EnumSet.copyOf(a);
		result.addAll(b);
		return result;
	}
}

?

@pfusik
Copy link
Collaborator

pfusik commented Mar 29, 2023

Ć Java
f1 == f2 f1.equals(f2)
~f EnumSet.complementOf(f)
f1 &= f2; f1 = EnumSet.copyOf(f1); f1.retainAll(f2);
f1 |= f2; f1 = EnumSet.copyOf(f1); f1.addAll(f2);
f1.HasFlag(f2) f1.containsAll(f2)
f1 ^ f2 (symmetric difference) ???

@crmorello
Copy link
Author

Ah nice, thanks for taking a look into this. The EnumSet solution looks good to me, though I don't write Java often so I'm not an authority on the matter. I think for symmetric difference most people generally rely on a library as there is no built in. There is two different implementations here to get an idea: https://rosettacode.org/wiki/Symmetric_difference#Java

@pfusik pfusik added the java label Aug 13, 2023
@pfusik
Copy link
Collaborator

pfusik commented Aug 17, 2023

EnumSet is problematic. Assigning (integer or other enum) values to Java enum is problematic.
For now, I'm going to emit Java enums only for non-flags enums with no assigned values. Other enums will remain interfaces with int constants.

@pfusik pfusik closed this as completed in b25821b Aug 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants