Data update
This commit is contained in:
parent
8e4e15fa56
commit
72eb4943cb
1853 changed files with 35514 additions and 9441 deletions
78
Task/Enumerations/Crystal/enumerations.cr
Normal file
78
Task/Enumerations/Crystal/enumerations.cr
Normal file
|
|
@ -0,0 +1,78 @@
|
|||
# An enum is a set of integer values, where each value has an associated name.
|
||||
# For example:
|
||||
|
||||
enum Color
|
||||
Red # 0
|
||||
Green # 1
|
||||
Blue # 2
|
||||
end
|
||||
|
||||
# Values start with the value 0 and are incremented by one,
|
||||
# but can be overwritten.
|
||||
|
||||
# To get the underlying value you invoke value on it:
|
||||
|
||||
Color::Green.value # => 1
|
||||
|
||||
# Each constant (member) in the enum has the type of the enum:
|
||||
|
||||
typeof(Color::Red) # => Color
|
||||
|
||||
# An enum can be marked with the @[Flags] annotation.
|
||||
# This changes the default values:
|
||||
|
||||
@[Flags]
|
||||
enum IOMode
|
||||
Read # 1
|
||||
Write # 2
|
||||
Async # 4
|
||||
end
|
||||
|
||||
# Additionally, some methods change their behaviour.
|
||||
|
||||
# An enum can be created from an integer:
|
||||
|
||||
Color.new(1).to_s # => "Green"
|
||||
|
||||
# Values that don't correspond to enum's constants are allowed:
|
||||
# the value will still be of type Color, but when printed you will get
|
||||
# the underlying value:
|
||||
|
||||
Color.new(10).to_s # => "10"
|
||||
|
||||
# This method is mainly intended to convert integers from C to enums in Crystal.
|
||||
|
||||
# An enum automatically defines question methods for each member,
|
||||
# using String#underscore for the method name.
|
||||
# * In the case of regular enums, this compares by equality (#==).
|
||||
# * In the case of flags enums, this invokes #includes?.
|
||||
# For example:
|
||||
|
||||
color = Color::Blue
|
||||
color.red? # => false
|
||||
color.blue? # => true
|
||||
|
||||
mode = IOMode::Read | IOMode::Async
|
||||
mode.read? # => true
|
||||
mode.write? # => false
|
||||
mode.async? # => true
|
||||
|
||||
# This is very convenient in case expressions:
|
||||
|
||||
case color
|
||||
when .red?
|
||||
puts "Got red"
|
||||
when .blue?
|
||||
puts "Got blue"
|
||||
end
|
||||
|
||||
# The type of the underlying enum value is Int32 by default,
|
||||
# but it can be changed to any type in Int::Primitive.
|
||||
|
||||
enum Color : UInt8
|
||||
Red
|
||||
Green
|
||||
Blue
|
||||
end
|
||||
|
||||
Color::Red.value # : UInt8
|
||||
|
|
@ -5,13 +5,13 @@ enum
|
|||
end
|
||||
type ExplicitFruits
|
||||
enum
|
||||
int APPLE = 10
|
||||
int BANANA = 20
|
||||
int CHERRY = 1
|
||||
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("[", Generic.name(enumeration), "]")
|
||||
writeLine("getting an object with value = 1:")
|
||||
writeLine(:enumeration.byValue(1))
|
||||
writeLine("iterating over the items:")
|
||||
|
|
|
|||
|
|
@ -1,7 +1,29 @@
|
|||
Module Checkit {
|
||||
\\ need revision 15, version 9.4
|
||||
Enum Fruit {apple, banana, cherry}
|
||||
Enum Fruit2 {apple2=10, banana2=20, cherry2=30}
|
||||
Enum Fruit {
|
||||
apple, banana, cherry
|
||||
}
|
||||
Enum Fruit2 {
|
||||
apple2=10,
|
||||
banana2=20.5,
|
||||
cherry2=30
|
||||
}
|
||||
Enum StrType {
|
||||
alfa="Άλφα",
|
||||
beta="Βήτα",
|
||||
gamma="Γάμμα",
|
||||
delta="Δέλτα"
|
||||
}
|
||||
Print alfa, beta
|
||||
z=alfa
|
||||
z++
|
||||
Print z=beta, eval$(z)="beta", z="Βήτα"
|
||||
z="Γάμμα" ' change to 3rd by searching value from the list
|
||||
CheckByReference2(&z)
|
||||
z1=Each(StrType, -1, 1) ' from last to first
|
||||
while z1
|
||||
CheckByValue2(z1)
|
||||
end while
|
||||
|
||||
Print apple, banana, cherry
|
||||
Print apple2, banana2, cherry2
|
||||
Print Len(apple)=0
|
||||
|
|
@ -47,10 +69,16 @@ Module Checkit {
|
|||
Sub CheckByValue(z as Fruit2)
|
||||
Print Eval$(z), z
|
||||
End Sub
|
||||
|
||||
Sub CheckByValue2(z as StrType)
|
||||
Print Eval$(z), z
|
||||
End Sub
|
||||
Sub CheckByReference(&z as Fruit2)
|
||||
z++
|
||||
Print Eval$(z), z
|
||||
End Sub
|
||||
Sub CheckByReference2(&z as StrType)
|
||||
z++
|
||||
Print Eval$(z), z
|
||||
End Sub
|
||||
}
|
||||
Checkit
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue