Guice and kotlin in-operator



I’m experimenting with Kotlin, Wicket and Guice.

I have a trait implemented by a class (class MyServiceImpl : MyService). I tried to wire this up in my guice module by:

class MyModule : AbstractModule() {   protected override fun configure() {   bind(javaClass<MyService>).to(javaClass<MyServiceImpl>).in(Scopes.SINGLETON);   } }

This causes multiple problems:

  1. the “to” gets confused with the global “” function, would “import as kotlinTo” solve this?
  2. the method “in” conflicts with the in-operator in Kotlin, how can I call a java-objects with methods named “in”?

/Tobias W


1. The 'to' problem seems to be a bug: is a member, so the to() extension function should not interfere. Please, file an issue about it. 2. To work around the 'in' problem, use backtics around it: `in`


Issue posted as:


Just as a reference for the future:

Turns out that this wasn’t a bug at all but me being a noob. :slight_smile: See Svetlana’s comment:

If I understand it correctly the kotlin to-extension-method was resolved because the return value of the “bind” method is nullable.

The correct code should be using ‘!!’, ‘?’ or ‘.sure()’:

class MyModule : AbstractModule() {   protected override fun configure() {   bind(javaClass<MyService>)!!.to(javaClass<MyServiceImpl>)!!.in(Scopes.SINGLETON);   } }

/Tobias W