Is it possible to make something like `run` or `wi...
# announcements
n
Is it possible to make something like
run
or
with
work with an enum class? basically a way to introduce a scope where we can reference the enumerators without qualification
s
are you thinking of something like Swift’s dot syntax for enums?
n
Not familiar
I just want to do:
Copy code
EnumName.run {
   if (x == EnumeratorOne) ...
}
which does not work
s
Unfortunately not, I think really the only thing you can do is import the members explicitly
import com.foo.EnumName.EnumeratorOne...
you can use a wildcard import or just let the IDE handle imports for you
w
it could work... with a companion object and a copy of each value in the enum
s
true, but that’d be a lot more boilerplate
w
true
s
either way, there’s no built in language construct to simplify references to enum values
The 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.
w
TypeScript also has this feature... as enums in TypeScript are pretty much a companion object with the values as members anyway... I think
in any case, just to clarify my earlier heresy:
Copy code
enum class Test
{
	A,
	B,
	C,;
	
	companion object
	{
		val a get() = A
		val b get() = B
		val c get() = C
	}
}
then you can do:
Copy code
with(Test) {
	val x = a
}
to do this just to be able to use with(Test) might be too much work
but it might be interesting to build a compiler plugin that generates the companion object
also... not sure how the naming would work...
I cant get them to be the exact same name
n
Yeah the problem with imports of course is that you can only import at file scope
so it would be file wide
i figured a companion object could help with this