Skip to content
dmiller edited this page Dec 20, 2011 · 1 revision

Working with enum types is straightforward. We have provided a few core methods to simplify certain operations.

Accessing/creating enum values

An enum type is value type derived from System.Enum. The named constants in an enum type are implemented as static fields on the type and hence are accessible using the regular syntax:

(import 'System.IO.FileMode)    ;=> System.IO.FileMode
FileMode/CreateNew              ;=> CreateNew

These are not integral values. They retain the enumeration type.

(class FileMode/CreateNew)      ;=> System.IO.FileMode

You can convert them to an integer value if you desire:

(int FileMode/CreateNew)       ;=> 1

If you want to convert from an integer value to an enumeration value, just do this:

(Enum/ToObject FileMode 4)     ;=> OpenOrCreate

If you want convert from the name of an integer to an enumeration value, the enum-val method will work with strings or anything that name works on:

(enum-val FileMode "CreateNew")  ;=> CreateNew
(enum-val FileMode :CreateNew)   ;=> CreateNew

Working with bit fields

Enumeration types that have the Flags attribute are often to represent bit fields. For convenience, we provide methods bit-or and bit-and to to combine and mask bit field values. For example, System.IO.FileShare has the Flags attribute. It is defined as follows:

[Serializable, ComVisible(true), Flags]
public enum FileShare
{
    Delete = 4,
    Inheritable = 0x10,
    None = 0,
    Read = 1,
    ReadWrite = 3,
    Write = 2
}

Use enum-or to combine values.

(import 'System.IO.FileShare)
(enum-or FileShare/Read FileShare/Write)          ;=> ReadWrite

Use enum-and to mask values.

(def r (enum-or FileShare/ReadWrite FileShare/Inheritable))
(= (enum-and r FileShare/Write) FileShare/Write)  ;=> true
(= (enum-and r FileShare/Write) FileShare/None)   ;=> false
(= (enum-and r FileShare/Delete) FileShare/None)  ;=> true

You can also use the HasFlag method to test:

(.HasFlag r FileShare/Write)                      ;=> true
(.HasFlag r FileShare/Delete)                     ;=> false
Clone this wiki locally