Guice and kotlin in-operator


#1

Hello,

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 “kotlin.to” function, would “import kotlin.to 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


#2

1. The 'to' problem seems to be a bug: Binder.to() 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`


#3

Issue posted as: http://youtrack.jetbrains.com/issue/KT-1874


#4

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: http://youtrack.jetbrains.com/issue/KT-1874#comment=27-327499.

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