Enumerations
Currently if you have parameters or attributes which can only contain one of a range of values, you have to write code to ensure that the value you're setting is valid - the compiler doesn't ensure the consistency of this data.
For example if a method expected to be passed days of the week represented by the values 1-7, you have no choice but to make the parameter an Integer which means there's nothing stopping a caller from passing 8, -1 or 1000000.
Global Constants don't solve this problem because even if you had global constants for Monday-Sunday the parameter would still be an Integer and there is no compile-time check that callers are passing valid values.
There's also an aspect of developer efficiency, in that existing values that are effectively enumberations (like lock types) are just Integers and the IDE doesn't help the developer find out what valid values are - they must look it up in the documentation.
Casting would need to be implemented for situations like imports where the data is coming from an untyped 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.
This feature is pushing more type-checking from user code at runtime to the compiler at compile-time.
I submitted this as an NFS in 2002 (#26604).
HistoricalComments
John Richards 7/11/2017 2:24:59 PM This is being considered as part of a larger review of the language and compiler.
|