Can an attribute reference a C3Type enum?


#1

Java lets me to define an enum

public enum Language {
java,
javascript,
php,
html,
sql
}

c3 too
enum type Language {
java,
javascript,
php,
html,
sql
}

Java lets me use the enum type as a reference:
public class TestLanguage {
Language a = Language.html;
Language b = Language.java;
}

Does C3?


#2

Can you provide more context, what do you want to achieve?


#3

@jmdepaepe Yes you can use the TypeName.FieldName in your code to refer to the enum values. They are not true references and refer to enum elements as strings today


#4

@rohit.sureka is there a way to refer to enum values inside a stored calc expression, on another type in the package?


#5

If the field is declared as of an enum type, you can use the enum constant as an initializer:

type TestLanguage {
  a: string enum Language = html
}

When programming, you can use the value as a constant:

JS:
TestLanguage.make({ s: Language.html })


#6

Thanks! One added clarification to your example is that from my experiments, the “html” must be lowercase, even if originally it was declared like:

enum type Language {
  HTML = "some string value"
}

i.e. with capital letters.


#7

Nope, it should be in the declared case. (If not, it’s a bug.) Note that we have lots of examples using upper case identifiers (to make them more distinct).