Nir
12/08/2020, 3:58 PMrun
or with
work with an enum class? basically a way to introduce a scope where we can reference the enumerators without qualificationShawn
12/08/2020, 4:00 PMNir
12/08/2020, 4:00 PMNir
12/08/2020, 4:00 PMEnumName.run {
if (x == EnumeratorOne) ...
}
Nir
12/08/2020, 4:01 PMShawn
12/08/2020, 4:04 PMimport com.foo.EnumName.EnumeratorOne...
Shawn
12/08/2020, 4:05 PMWietlol
12/08/2020, 4:05 PMShawn
12/08/2020, 4:05 PMWietlol
12/08/2020, 4:05 PMShawn
12/08/2020, 4:05 PMShawn
12/08/2020, 4:06 PMShawn
12/08/2020, 4:06 PMThe type of directionToHead is inferred when it’s initialized with one of the possible values of CompassPoint. Once directionToHead is declared as a CompassPoint, you can set it to a different CompassPoint value using a shorter dot syntax:
directionToHead = .east
The type of directionToHead is already known, and so you can drop the type when setting its value. This makes for highly readable code when working with explicitly typed enumeration values.
Wietlol
12/08/2020, 4:06 PMWietlol
12/08/2020, 4:07 PMenum class Test
{
A,
B,
C,;
companion object
{
val a get() = A
val b get() = B
val c get() = C
}
}
Wietlol
12/08/2020, 4:07 PMwith(Test) {
val x = a
}
Wietlol
12/08/2020, 4:08 PMWietlol
12/08/2020, 4:08 PMWietlol
12/08/2020, 4:08 PMWietlol
12/08/2020, 4:08 PMNir
12/08/2020, 4:11 PMNir
12/08/2020, 4:11 PMNir
12/08/2020, 4:11 PM