Enumerations: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 129: | Line 129: | ||
writes 0, 1, 2 to the console. |
writes 0, 1, 2 to the console. |
||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
AutoHotkey doesn't really enforce types. <br> |
AutoHotkey doesn't really enforce types. <br> |
||
Line 208: | Line 209: | ||
typedef enum { apple = 0, banana = 1, cherry = 2 } fruits;</lang> |
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++}}== |
=={{header|C++}}== |
||
Line 229: | Line 243: | ||
You can also explicitly specify an underlying type for old-style enums: |
You can also explicitly specify an underlying type for old-style enums: |
||
<lang cpp>enum fruits : unsigned int { apple, banana, cherry };</lang> |
<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}}== |
=={{header|Clojure}}== |
||
Line 256: | Line 257: | ||
(println (fruit? :apple)) |
(println (fruit? :apple)) |
||
(println (fruit-value :banana))</lang> |
(println (fruit-value :banana))</lang> |
||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
Values: |
Values: |
||
Line 452: | Line 453: | ||
For the unspecific value enum use case, Erlang has atoms. You can use apple, banana, orange directly in the code. |
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} |
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}}== |
=={{header|Factor}}== |
||
Line 553: | Line 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...) |
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}}== |
=={{header|FreeBASIC}}== |
||
Line 578: | Line 605: | ||
1 2 4 |
1 2 4 |
||
</pre> |
</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}}== |
=={{header|FutureBasic}}== |
||
Line 699: | Line 700: | ||
CHERRY |
CHERRY |
||
}</lang> |
}</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}}== |
=={{header|Inform 7}}== |
||
Line 718: | Line 729: | ||
banana 2 |
banana 2 |
||
cherry 3</lang> |
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}}== |
=={{header|J}}== |
||
Line 805: | Line 806: | ||
def fruits: {apple, banana, cherry}; # i.e. {"apple" : null, "banana": null, "cherry": null } |
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}}== |
=={{header|JScript.NET}}== |
||
<lang jscript>enum fruits { apple, banana, cherry } |
<lang jscript>enum fruits { apple, banana, cherry } |
||
enum fruits { apple = 0, banana = 1, cherry = 2 }</lang> |
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}}== |
=={{header|Julia}}== |
||
Line 942: | Line 943: | ||
Although since Lua strings are interned, there is as much benefit to simply using strings. |
Although since Lua strings are interned, there is as much benefit to simply using strings. |
||
=={{header|M2000 Interpreter}}== |
=={{header|M2000 Interpreter}}== |
||
<lang M2000 Interpreter> |
<lang M2000 Interpreter> |
||
Line 1,001: | Line 1,003: | ||
Checkit |
Checkit |
||
</lang> |
</lang> |
||
=={{header|M4}}== |
=={{header|M4}}== |
||
Line 1,015: | Line 1,016: | ||
c=3 |
c=3 |
||
</pre> |
</pre> |
||
=={{header|Mathematica}}== |
=={{header|Mathematica}}== |
||
Line 1,030: | Line 1,030: | ||
G |
G |
||
->{4}</lang> |
->{4}</lang> |
||
=={{header|MATLAB}} / {{header|Octave}}== |
=={{header|MATLAB}} / {{header|Octave}}== |
||
Line 1,056: | Line 1,055: | ||
end</lang> |
end</lang> |
||
=={{header|Modula-3}}== |
=={{header|Modula-3}}== |
||
<lang modula3>TYPE Fruit = {Apple, Banana, Cherry};</lang> |
<lang modula3>TYPE Fruit = {Apple, Banana, Cherry};</lang> |
||
Line 1,197: | Line 1,197: | ||
# Using a hash |
# Using a hash |
||
my %fruits = ( apple => 0, banana => 1, cherry => 2 );</lang> |
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}}== |
=={{header|Phix}}== |
||
Line 1,427: | Line 1,415: | ||
((ctype-c->scheme _fruits) 5) ; -> '(APPLE CHERRY) |
((ctype-c->scheme _fruits) 5) ; -> '(APPLE CHERRY) |
||
</lang> |
</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}}== |
=={{header|Raven}}== |
||
Line 1,584: | Line 1,585: | ||
} |
} |
||
</lang> |
</lang> |
||
=={{header|Scheme}}== |
=={{header|Scheme}}== |
||
<lang scheme>(define apple 0) |
<lang scheme>(define apple 0) |
||
Line 1,594: | Line 1,596: | ||
(equal? 'cherry atom)))</lang> |
(equal? 'cherry atom)))</lang> |
||
(This section needs attention from someone familiar with Scheme idioms.) |
(This section needs attention from someone familiar with Scheme idioms.) |
||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
<lang seed7>const type: fruits is new enum |
<lang seed7>const type: fruits is new enum |
||
apple, banana, cherry |
apple, banana, cherry |
||
end enum;</lang> |
end enum;</lang> |
||
=={{header|Shen}}== |
=={{header|Shen}}== |
||
<lang shen>(tc +) |
<lang shen>(tc +) |
||
Line 1,635: | Line 1,639: | ||
Using a dictionary: |
Using a dictionary: |
||
<lang slate>define: #fruit &builder: [{#Apple -> 1. #Banana -> 2. #Cherry -> 3} as: Dictionary].</lang> |
<lang slate>define: #fruit &builder: [{#Apple -> 1. #Banana -> 2. #Cherry -> 3} as: Dictionary].</lang> |
||
=={{header|Standard ML}}== |
=={{header|Standard ML}}== |
||
<lang sml>datatype fruit = |
<lang sml>datatype fruit = |