The datatype enumFebruary 03. 2012
IntroductionWirbel provides a special datatype for enumeration values. It creates an independent namespace of globally unique symbols. There are no reserved words, not even those of the builtin keywords. Enum literals are names prefixed with one backquote, e.g.: a = `yellow b = `green Wirbel guarantees that all enum values are different. In the target C++ code enums are implemented via a 32 bit unsigned integer, so they are very efficient. All string processing is done at compile time. OperatorsThe following operators are available on enumeration values: ==, !=, <=, >=, <, > and is. The function int(`enum) lets you access the enum's internal number representation and is intended for debugging purposes only. The relational operators <=, >=, < and > can be used in situations, where enumeration values need to be sorted (for example in dictionaries). You cannot rely upon a certain enumeration literal being less or greater than another. Wirbel only guarantees that all enumeration literals are ordered and that the order does not change during run time. It can change bewteen two compile runs, however. The is operator behaves exactly like == since enumeration value are immutable - just like integers. Enumeration values can be null, which also is the default value of the enum datatype. An ExampleLet's look at the following example: The fictive function ask_user asks the user wether he wants to do a certain action. There are three possible answers: ask_user.w
def ask_user():
do_ask_user_something()
if user_says_yes():
return `yes
elif user_says_no():
return `no
else:
return `cancel
There is no need to declare `yes, `no and `cancel anywhere. A caller of ask_user simply compares the enum values using the operators == and !=: answer = ask_user() if answer == `yes: save_file() elif answer == `no: quit() |
| |||||||||||||||||||||||||||||||||||||||