
-----------------------------------
wtd
Mon Sep 27, 2004 2:19 pm

Just curious... enums?
-----------------------------------
I'm curious, I presume Turing provides an "enum" capability, but can't seem to find any references to how to create one.  Anyone have the answer to this conundrum?

-----------------------------------
Mazer
Mon Sep 27, 2004 2:49 pm


-----------------------------------
Heh. I guess it does. I never actually thought about it until you suggested using it in someone else's post. But here, from the reference:


enum                                                     enumerated type

Syntax   An enumeratedType is:
  enum (id { , id } ) 


 
Description   The values of an enumerated type are distinct and increasing. They can be thought of as the values 0, 1, 2 and so on, but arithmetic is not allowed with these values.
 
Example   
        type color : enum (red, green, blue)
        var c : color := color . red
        var d : color := succ (c)       % d becomes green
 
Details   Each value of an enumerated type is the name of the type followed by a dot followed by the element's name, for example, color.red. Enumerated values can be compared for equality and for ordering. The succ and pred functions can be used to find the value following or preceding a given enumerated value. The ord function can be used to find the enumeration position of a value, for example, ord (color.red) is 0.
Enumerated types cannot be combined with integers or with other enumerated types.

 
Details   It is illegal to declare an "anonymous" enum. The only legal declaration for an enum is in a type declaration. For example, the following is now illegal:

        var a : array enum (red, green, blue) of int
Given that there is no (easy) way of generating an enum value without it being a named type, this should not impact any but the most bizarre code.
 
Details   The "put" and "get" statement semantics have been expanded to allow put's and get's of enum values. The values printed and input are the element names themselves, case sensitive. For example, for

        type colors : enum (red, green, blue)
        var c : colors := colors . red
        put c       % outputs "red" (without the quotes)
 

-----------------------------------
wtd
Mon Sep 27, 2004 3:04 pm


-----------------------------------
Great.  Thanks.

-----------------------------------
Jonny Tight Lips
Mon Sep 27, 2004 4:45 pm


-----------------------------------
I have a question. What is the use of this command? I don't really understand its usefulness?

-----------------------------------
wtd
Mon Sep 27, 2004 6:33 pm


-----------------------------------
Consider if you have an array of events called "week" (with one event per week), and you want to be able to look them up in some meaningful way.

var Events : array 0..6 of string
Events(0) := "It's Sunday... do something Sabbath-y."

put Events(0)

vs.

type Days = enum (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
var events : array 0..6 of string
events(Sunday) := "It's Sunday... do something Sabbath-y."

put events(Sunday)

Which do you prefer to look at?  :)

-----------------------------------
Cervantes
Mon Sep 27, 2004 6:51 pm


-----------------------------------
In turing, the code would look like this:


type Days : enum (Sunday, Monday, Tuesday, Wednesday, Thursday, Friday, Saturday)
var events : array 0 .. 6 of string

events (ord (Days.Sunday)) := "It's Sunday... do something Sabbath-y."

put events (ord (Days.Sunday))


the question, which would you prefer to look at, is becoming less one-sided since we have to put those ords in.  :?

-----------------------------------
wtd
Mon Sep 27, 2004 8:37 pm


-----------------------------------
Weird.
