Enumerations: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 129:
 
writes 0, 1, 2 to the console.
 
=={{header|AutoHotkey}}==
AutoHotkey doesn't really enforce types. <br>
Line 208 ⟶ 209:
 
typedef enum { apple = 0, banana = 1, cherry = 2 } fruits;</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>enum fruits { apple, banana, cherry }
 
enum fruits { apple = 0, banana = 1, cherry = 2 }
 
enum fruits : int { apple = 0, banana = 1, cherry = 2 }
 
[FlagsAttribute]
enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }</lang>
 
Placing FlagsAttribute before an enum allows you to perform bitwise operations on the value.
Note: All enums have a value of 0 defined, even if not specified in the set values.
 
=={{header|C++}}==
Line 229 ⟶ 243:
You can also explicitly specify an underlying type for old-style enums:
<lang cpp>enum fruits : unsigned int { apple, banana, cherry };</lang>
 
=={{header|C sharp|C#}}==
<lang csharp>enum fruits { apple, banana, cherry }
 
enum fruits { apple = 0, banana = 1, cherry = 2 }
 
enum fruits : int { apple = 0, banana = 1, cherry = 2 }
 
[FlagsAttribute]
enum Colors { Red = 1, Green = 2, Blue = 4, Yellow = 8 }</lang>
 
Placing FlagsAttribute before an enum allows you to perform bitwise operations on the value.
Note: All enums have a value of 0 defined, even if not specified in the set values.
 
=={{header|Clojure}}==
Line 256 ⟶ 257:
(println (fruit? :apple))
(println (fruit-value :banana))</lang>
 
=={{header|Common Lisp}}==
Values:
Line 452 ⟶ 453:
For the unspecific value enum use case, Erlang has atoms. You can use apple, banana, orange directly in the code.
If they have to have a specific value they could be grouped like this: {apple, 1}, {banana, 3}, {orange, 8}
 
=={{header|F_Sharp|F#}}==
Enumerations in F# always have explicit values:
<lang fsharp>type Fruit =
| Apple = 0
| Banana = 1
| Cherry = 2
 
let basket = [ Fruit.Apple ; Fruit.Banana ; Fruit.Cherry ]
Seq.iter (printfn "%A") basket</lang>
 
If the initialization values are omitted, the resulting type is a discriminated union (algebraic data type) instead.
Simple discriminated unions can be used similarly to enumerations, but they are never convertible from and to integers, and their internal representation is quite different.
 
<lang fsharp>type Fruit =
| Apple
| Banana
| Cherry
let basket = [ Apple ; Banana ; Cherry ]
Seq.iter (printfn "%A") basket</lang>
 
=={{header|Factor}}==
Line 553 ⟶ 574:
 
does not work with gfortran; it is used in some [http://docs.cray.com/books/S-3692-51/html-S-3692-51/z970507905n9123.html Cray docs] about Fortran, but the syntax shown at [http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlf101a.doc/xlflr/enum.htm IBM] is the one gfortran can understand. (Cray's docs refer to Fortran 2003 draft, IBM docs refers to Fortran 2003 standard, but read the brief [http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/topic/com.ibm.xlf101a.doc/xlflr/languagestandards.htm#wq17 Fortran 2003 Standard] section to understand why differences may exist...)
 
=={{header|Free Pascal}}==
See [[#Delphi|Delphi]].
Note, depending on the <tt>{$scopedEnum}</tt> compiler switch (as of definition time), enumeration type members are identified via the type name prepended.
 
Additionally, enumeration types can be passed to <tt>write</tt>/<tt>writeLn</tt> producing the Pascal (source code) identifier.
 
=={{header|FreeBASIC}}==
Line 578 ⟶ 605:
1 2 4
</pre>
 
=={{header|Free Pascal}}==
See [[#Delphi|Delphi]].
Note, depending on the <tt>{$scopedEnum}</tt> compiler switch (as of definition time), enumeration type members are identified via the type name prepended.
 
Additionally, enumeration types can be passed to <tt>write</tt>/<tt>writeLn</tt> producing the Pascal (source code) identifier.
 
=={{header|F_Sharp|F#}}==
Enumerations in F# always have explicit values:
<lang fsharp>type Fruit =
| Apple = 0
| Banana = 1
| Cherry = 2
 
let basket = [ Fruit.Apple ; Fruit.Banana ; Fruit.Cherry ]
Seq.iter (printfn "%A") basket</lang>
 
If the initialization values are omitted, the resulting type is a discriminated union (algebraic data type) instead.
Simple discriminated unions can be used similarly to enumerations, but they are never convertible from and to integers, and their internal representation is quite different.
 
<lang fsharp>type Fruit =
| Apple
| Banana
| Cherry
let basket = [ Apple ; Banana ; Cherry ]
Seq.iter (printfn "%A") basket</lang>
 
=={{header|FutureBasic}}==
Line 699 ⟶ 700:
CHERRY
}</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Nether Icon nor Unicon has an explicit enumeration type; however, there are several approaches that can be used for this purpose:
 
<lang Icon> fruits := [ "apple", "banana", "cherry", "apple" ] # a list keeps ordered data
fruits := set("apple", "banana", "cherry") # a set keeps unique data
fruits := table() # table keeps an unique data with values
fruits["apple"] := 1
fruits["banana"] := 2
fruits["cherry"] := 3</lang>
 
=={{header|Inform 7}}==
Line 718 ⟶ 729:
banana 2
cherry 3</lang>
 
=={{header|Icon}} and {{header|Unicon}}==
Nether Icon nor Unicon has an explicit enumeration type; however, there are several approaches that can be used for this purpose:
 
<lang Icon> fruits := [ "apple", "banana", "cherry", "apple" ] # a list keeps ordered data
fruits := set("apple", "banana", "cherry") # a set keeps unique data
fruits := table() # table keeps an unique data with values
fruits["apple"] := 1
fruits["banana"] := 2
fruits["cherry"] := 3</lang>
 
=={{header|J}}==
Line 805 ⟶ 806:
 
def fruits: {apple, banana, cherry}; # i.e. {"apple" : null, "banana": null, "cherry": null }
 
=={{header|JSON}}==
<lang json>{"fruits" : { "apple" : null, "banana" : null, "cherry" : null }
{"fruits" : { "apple" : 0, "banana" : 1, "cherry" : 2 }</lang>
 
=={{header|JScript.NET}}==
<lang jscript>enum fruits { apple, banana, cherry }
enum fruits { apple = 0, banana = 1, cherry = 2 }</lang>
 
=={{header|JSON}}==
<lang json>{"fruits" : { "apple" : null, "banana" : null, "cherry" : null }
{"fruits" : { "apple" : 0, "banana" : 1, "cherry" : 2 }</lang>
 
=={{header|Julia}}==
Line 942 ⟶ 943:
 
Although since Lua strings are interned, there is as much benefit to simply using strings.
 
=={{header|M2000 Interpreter}}==
<lang M2000 Interpreter>
Line 1,001 ⟶ 1,003:
Checkit
</lang>
 
 
=={{header|M4}}==
Line 1,015 ⟶ 1,016:
c=3
</pre>
 
 
=={{header|Mathematica}}==
Line 1,030:
G
->{4}</lang>
 
 
=={{header|MATLAB}} / {{header|Octave}}==
Line 1,056 ⟶ 1,055:
 
end</lang>
 
=={{header|Modula-3}}==
<lang modula3>TYPE Fruit = {Apple, Banana, Cherry};</lang>
Line 1,197:
# Using a hash
my %fruits = ( apple => 0, banana => 1, cherry => 2 );</lang>
 
=={{header|Perl 6}}==
{{works with|Rakudo|2016.01}}
 
<lang perl6>enum Fruit <Apple Banana Cherry>; # Numbered 0 through 2.
 
enum ClassicalElement (
Earth => 5,
'Air', # gets the value 6
'Fire', # gets the value 7
Water => 10,
);</lang>
 
=={{header|Phix}}==
Line 1,427 ⟶ 1,415:
((ctype-c->scheme _fruits) 5) ; -> '(APPLE CHERRY)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2016.01}}
 
<lang perl6>enum Fruit <Apple Banana Cherry>; # Numbered 0 through 2.
 
enum ClassicalElement (
Earth => 5,
'Air', # gets the value 6
'Fire', # gets the value 7
Water => 10,
);</lang>
 
=={{header|Raven}}==
Line 1,584 ⟶ 1,585:
}
</lang>
 
=={{header|Scheme}}==
<lang scheme>(define apple 0)
Line 1,594 ⟶ 1,596:
(equal? 'cherry atom)))</lang>
(This section needs attention from someone familiar with Scheme idioms.)
 
=={{header|Seed7}}==
<lang seed7>const type: fruits is new enum
apple, banana, cherry
end enum;</lang>
 
=={{header|Shen}}==
<lang shen>(tc +)
Line 1,635 ⟶ 1,639:
Using a dictionary:
<lang slate>define: #fruit &builder: [{#Apple -> 1. #Banana -> 2. #Cherry -> 3} as: Dictionary].</lang>
 
=={{header|Standard ML}}==
<lang sml>datatype fruit =
10,327

edits