Enumerations: Difference between revisions

m
(Added Odin variant)
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by one other user not shown)
Line 673:
fruits = ~w(apple banana cherry)a |> Enum.with_index |> Map.new
#=> %{apple: 0, banana: 1, cherry: 2}</syntaxhighlight>
 
=={{header|EMal}}==
<syntaxhighlight lang="emal">
in Org:RosettaCode
type Fruits
enum
int APPLE, BANANA, CHERRY
end
type ExplicitFruits
enum
int APPLE = 10
int BANANA = 20
int CHERRY = 1
end
type Main
for each generic enumeration in generic[Fruits, ExplicitFruits]
writeLine("[" + Generic.name(enumeration) + "]")
writeLine("getting an object with value = 1:")
writeLine(:enumeration.byValue(1))
writeLine("iterating over the items:")
for each var fruit in :enumeration
writeLine(fruit)
end
writeLine()
end
</syntaxhighlight>
{{out}}
<pre>
[Org:RosettaCode:Fruits]
getting an object with value = 1:
BANANA(1)
iterating over the items:
APPLE(0)
BANANA(1)
CHERRY(2)
 
[Org:RosettaCode:ExplicitFruits]
getting an object with value = 1:
CHERRY(1)
iterating over the items:
APPLE(10)
BANANA(20)
CHERRY(1)
</pre>
 
=={{header|Erlang}}==
Line 2,135 ⟶ 2,179:
 
The only way to give such a variable a value without setting it explicitly is to add one to the previous such variable which (in effect) is what a C-style enum does. If you declare a variable in Wren without giving it a value, then it is set to the special value ''null'' which is no help here.
<syntaxhighlight lang="ecmascriptwren">var APPLE = 1
var ORANGE = 2
var PEAR = 3
Line 2,153 ⟶ 2,197:
<br>
{{libheader|Wren-dynamic}}
<syntaxhighlight lang="ecmascriptwren">import "./dynamic" for Enum
 
var Fruit = Enum.create("Fruit", ["apple", "orange", "pear", "cherry", "banana", "grape"], 1)
9,476

edits