function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Set<enum type> not allowed

A common pattern in Java is to declare an enum, and then use Set to declare subsets of the enum.  The subsets can then be used to validate that they contain a value passed as the enum type.  


For instance:





// Define commands that only require the AccountNumber to operate
public static Set<MyClass.Command> accountNumberOnlyCommands;

static {




would allow you to then do:



public MyClass(Command thisCommand) {
if (accountNumberOnlyCommands.contains( thisCommand ) )




But, when you try to declare your Set<enum type>, it creates a build error of "Set of <enum type> not allowed".  Why would it not allow this?  After all, an enum, besides being a type declaration, is conceptual a set of unique values. It is only logical to be able to use Set to declare subsets and then the contains method to verify that a value is a member of the subset.  And, like I said, you can do this in Java. 


If you change Set to List in the declaration, it allows it, so you can create lists of an enum type.  But, List doesn't have the contains method, so it then errors on the if statement above.  


Does anyone know how to resolve this?  


Message Edited by ErikOSnet0 on 12-01-2009 10:35 AM

I think sets were designed not to accept non-primitives since Apex offers no way of evaluating the equality of two objects, which a Set needs to function.  You are right that enum being a unique set of values, could have been included in the list of types allowed by Set. But unfortunately it's not working that way today. I would suggest you post an idea in IdeaExchange and I am sure you would find people promoting it.


Now coming to the second question how you can work around it, use a List and implement your own "contains" method.