Page 1 of 1

Enumerations in Jade?

Posted: Fri Aug 07, 2009 1:05 pm
by ConvertFromOldNGs
by Matt >> Wed, 29 Nov 2006 21:58:12 GMT

Hi, Jade doesn't seem to have native support for enumerations (or am I wrong?). If not, any tips on howto achieve this?

For example, I want something like -

RV_TYPE = (NORMAL, BINOMIAL,...etc) and then access this in code using

.....RV_TYPE.NORMAL..... (would be intger value of 1)

Thanks. Matt.

Re: Enumerations in Jade?

Posted: Fri Aug 07, 2009 1:05 pm
by ConvertFromOldNGs
by dcooper@jade.co.nz >> Wed, 29 Nov 2006 22:09:31 GMT

I've seen people use abstract classes and class constants to mimic enumerations. For example:

EnumClass (abstract base class)
RV_TYPE (abstract subclass)

On RV_TYPE, define class constants for NORMAL (1), BINOMIAL (2), etc.

Now you can write code like:

dist := RV_TYPE.NORMAL;

The classes don't need to be abstract for this to work. But making them abstract ensures that no instances will ever be created, which helps to ensure that the classes are used only for enumeration purposes.

Cheers,
Dean.

Re: Enumerations in Jade?

Posted: Fri Aug 07, 2009 1:05 pm
by ConvertFromOldNGs
by Ben >> Thu, 30 Nov 2006 3:01:44 GMT

Would using Global Constants help here at all?
It may not have the same syntax, but would give the same result.

e.g.
RV_TYPES (for the Catgeory)
RVTYPE_Normal (with Integer value 1)
RVTYPE_Binomal (with Integer value 2)
etc.

Re: Enumerations in Jade?

Posted: Fri Aug 07, 2009 1:05 pm
by ConvertFromOldNGs
by Michael J >> Fri, 1 Dec 2006 2:59:55 GMT

Personally I always avoid using global constants. In OO terms I cannot see a scenario that would warrant making a constant global.

An abstract class based enumeration as dean described above sounds like best approach.

Re: Enumerations in Jade?

Posted: Fri Aug 07, 2009 1:05 pm
by ConvertFromOldNGs
by Anonymous >> Tue, 5 Dec 2006 3:18:57 GMT
Personally I always avoid using global constants. In OO terms I cannot see a scenario that would warrant making a constant global.

In OO terms, GC's could be considered as constants belonging to the Object class.

Re: Enumerations in Jade?

Posted: Fri Aug 07, 2009 1:05 pm
by ConvertFromOldNGs
by Michael J >> Tue, 5 Dec 2006 21:05:54 GMT

Yes I agree with that, however my point wasnt that there is not a functional equivalent in abstract OO tems but rather that there is rarely (if ever) a justification for making a constant global.

Re: Enumerations in Jade?

Posted: Fri Aug 07, 2009 1:05 pm
by ConvertFromOldNGs
by John Munro >> Mon, 18 Dec 2006 22:04:02 GMT

It's not really the same result because it doesn't implement a compile-time check and can't be used as a helper in the IDE.

For example if you had a method that took an RV_TYPE as a parameter

meth(rv : RV_TYPE);

the compiler could check callers of this method to make sure that only an RV_TYPE was passed to it. If you just used global constants the parameter would need to be an Integer

meth(rv : Integer);

and you would need to add code to make sure that the Integer that is passed in is a valid RV_TYPE.

Casting would need to be implemented for situations like imports where the data is coming from a typeless source. In this case if the data loaded from the import file did not represent a valid enumeration value a casting exception would be thrown.

Basically it's just pushing type-checking as much as possible to the compiler and compile-time rather than user code at runtime.

The IDE helper would make developer's lives easier by providing a pop-up combo similar to the current ctrl+3 one, but would detect that the cursor is in a position where it would be appropriate to show the values for the enumeration. For example if you typed

meth(

and pressed ctrl+3 the IDE might show you a pop-up combo with the items RVTYPE_Normal and RVTYPE_Binomal in it.

The above compiler checks and IDE helper would also apply to setting the value of an attribute.

Incidentally I submitted an NFS in 2002 for enumerations (#26604). I guess the Jade plant either decided it was a very low priority or a bad idea.

John