How do you access a Companion from Groovy? ```clas...
# getting-started
h
How do you access a Companion from Groovy?
Copy code
class Foo {
  companion object {
  val s = "foo"
  }
}
results into
Foo.Companion.s
but the static field
Companion
clashes with the class
Companion
resulting in
Companion::class.s
and an unresolved member
s
due Groovy class syntax.
k
Maybe
<mailto:Foo.@Companion.xyz|Foo.@Companion.xyz>
?
👆 1
h
What kind of magic is this? 😄
But yes, it does work, thank you!
v
The
.@
in Groovy means "access the field with that name directly". If you for example have a property that consist of a field "foo" and a getter "getFoo", then in Groovy using
.foo
will call the
getFoo
getter, but with
.@foo
you can directly access the field without going through the getter. Same works here for the companion object to access the static field called
Companion
instead of the nested class called
Companion
.
h
Thanks for the explanation. Don't know why I did found it when I tried to duckduckgo it... Interesting
Foo.Companion.s
does work when you don't use
CompileStatic
, but I want to enable it.