I have a proposal stemming from a newly-reported issue: [LDEV-554] - Lucee
I want to make an enumerated type. For instance, in Java:
// NamedColor.java
enum NamedColor {
RED(255,0,0),
BLUE(0,255,0),
GREEN(0,0,255);
private int r;
private int g;
private int b;
NamedColor(int r,int g,int b) {
this.r = r;
this.g = g;
this.b = b;
}
}
The above results in a construct so that NamedColor.RED instanceof NamedColor == true.
I tried to replicate the above using public final static members and creating the new instances, but that lead to the above bug report.
I would like to see a true enumerated type in the Lucee dialect. Not sure it would fit in the CFML dialect though.
// NamedColor.lucee
enum {
RED(255,0,0),
GREEN(0,255,0),
BLUE(0,0,255);
private r;
private g;
private b;
private function init(r, g, b) {
this.r = r;
this.g = g;
this.b = b;
}
}
This might just be some syntatic sugar that accomplishes the same thing as I tried to do in the above bug report.
More Examples:
// DayOfTheWeek.lucee
enum {
SUNDAY,
MONDAY,
TUESDAY,
WEDNESDAY,
THURSDAY,
FRIDAY,
SATURDAY;
}
// Planet.lucee
enum {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7);
static {
// universal gravitational constant (m3 kg-1 s-2)
private final G = 6.67300E-11;
}
private final mass; // in kilograms
private final radius; // in meters
private function init(numeric mass, numeric radius) {
this.mass = mass;
this.radius = radius;
}
public numeric function mass() {
return this.mass;
}
public numeric function radius() {
return this.radius;
}
public numeric function surfaceGravity() {
return static.G * mass / (radius * radius);
}
public numeric function surfaceWeight(numeric otherMass) {
return otherMass * surfaceGravity();
}
}
Example Usage
// index.lucee
<:script>
today = DayOfTheWeek::MONDAY;
var i=1;
for (day in DayOfTheWeek::values()) {
echo(day.name() & (i++%2==0) ? ', happy days. ' : ', ');
}
echo('what a day, groovin all week with you!');
day = DayOfTheWeek::valueOf(form.day);
if (!(day instanceof DayOfTheWeek)) { // get either a day or null
throw("Invalid day - #form.day#");
}
echo('If you weigh 81kg Earth, you would weigh #Planet::MARS.surfaceWeight(81)#kg on Mars.');
</:script>