Be concise.
Be useful.
All contributions dictatorially edited by webmasters to match personal tastes.
Please do not paste any copyright violating material.
Please try to avoid dependencies to third-party libraries and frameworks.
- Python
- Python
- Ada
- C
- Caml
- Clojure
- Clojure
- C++
- C++
- C#
- D
- Dart
- Elixir
- Fortran
- Go
- Groovy
- Haskell
- JS
- Java
- Lua
- Obj-C
- PHP
- PHP
- Pascal
- Perl
- Ruby
- Rust
- Scala
- VB
class Suit(Enum):
SPADES = 1
HEARTS = 2
DIAMONDS = 3
CLUBS = 4
New in Python 3.4
class Suit:
SPADES, HEARTS, DIAMONDS, CLUBS = range(4)
Fake enum, works with any version of python.
type Suit is (Spades, Hearts, Diamonds, Clubs);
enum Suit {
SPADES,
HEARTS,
DIAMONDS,
CLUBS
};
type suit = Spades | Hearts | Diamonds | Clubs
(def suit #{:SPADES :HEARTS :DIAMONDS :CLUBS})
#{} - is a hashset,
keywords are case sensitive.
keywords are case sensitive.
enum class Color : char{
Red, Black, Green
};
You can determine a type.
enum class Suit {
SPADES, HEARTS, DIAMONDS, CLUBS
};
Using enum class avoids name collisions
enum Suit
{
Spades,
Hearts,
Diamonds,
Clubs
}
enum Suit{
SPADES,
HEARTS,
DIAMONDS,
CLUBS
}
@suits %{
"SPADES" => 1,
"HEARTS" => 2,
"DIAMONDS" => 3,
"CLUBS" => 4
}
def main, do: @suits
enum, bind(c)
enumerator :: spades, hearts, diamonds, clubs
end enum
enum Suit{
SPADES,
HEARTS,
DIAMONDS,
CLUBS
}
data Suit = SPADES | HEARTS | DIAMONDS | CLUBS deriving (Enum)
const spades = 0
const hearts = 1
const diamonds = 2
const clubs = 3
Similar to the latter python implementation, this a fake enum.
enum Suit{
SPADES,
HEARTS,
DIAMONDS,
CLUBS;
}
Suit = {
SPADES=1,
HEARTS=2,
DIAMONDS=3,
CLUBS=4
}
typedef NS_ENUM(int, Suit) {
SPADES, HEARTS, DIAMONDS, CLUBS
};
Unlike plain-C enum (which works in ObjC same way as in plain C), NS_ENUM allows to select the desired enumerated value type.
Type
Suit = (Spades, Hearts, Diamonds, Clubs);
my %suit = (
SPADES => 1,
HEARTS => 2,
DIAMONDS => 3,
CLUBS => 4,
);
Hashes can be used to create enums.
To retrieve the value:
print $suit{HEARTS} # -> 2
To retrieve the value:
print $suit{HEARTS} # -> 2
class Colors
include Ruby::Enum
define :SPADES, "spades"
define :HEARTS, "hearts"
define :DIAMONDS, "diamonds"
define :CLUBS, "clubs"
end
sealed trait Suit
case object Spades extends Suit
case object Hearts extends Suit
case object Diamonds extends Suit
case object Clubs extends Suit