Skip to content

Commit

Permalink
Patch TypeConverions to support Nothing type
Browse files Browse the repository at this point in the history
  • Loading branch information
cacoco committed May 21, 2021
1 parent 24beea1 commit 7ac471b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import scala.reflect.runtime.universe.{Type => ScalaType, _}
*/
private [scalaguice] object TypeConversions {
private val anyType = typeOf[Any]
private val nothingType = typeOf[Nothing]

object ArrayType {
private val arraySymbol = symbolOf[Array[_]]
Expand Down Expand Up @@ -55,12 +56,13 @@ private [scalaguice] object TypeConversions {
}

}

//https://github.com/scala/scala/blob/98b9a1c19ba2b7b342bc9b838628b86d00276540/src/reflect/scala/reflect/internal/Types.scala#L31

def scalaTypeToJavaType(scalaType: ScalaType, mirror: Mirror, allowPrimative: Boolean = false): JavaType = {
scalaType.dealias match {
case `anyType` => classOf[java.lang.Object]
case `anyType` => classOf[java.lang.Object]
case `nothingType` => classOf[scala.runtime.Nothing$]
case ExistentialType(_, underlying) => scalaTypeToJavaType(underlying, mirror)
case ArrayType(argType) => arrayOf(scalaTypeToJavaType(argType, mirror, allowPrimative=true))
case ClassType(symbol, args) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
package net.codingwell.scalaguice

import org.scalatest.{Matchers, WordSpec}

import com.google.inject._
import scala.util.Try

class BindingExtensionsSpec extends WordSpec with Matchers {

import BindingExtensions._

def module(body: Binder => Unit) = new Module {
def configure(binder: Binder) = body(binder)
def module(body: Binder => Unit): Module = new Module {
def configure(binder: Binder): Unit = body(binder)
}

"Binding extensions" should {
Expand Down Expand Up @@ -82,5 +82,18 @@ class BindingExtensionsSpec extends WordSpec with Matchers {
} getInstance classOf[SomeClazz]
inst.get should equal("String with trait augmentation")
}

"allow binding with Nothing type" in {
import net.codingwell.scalaguice.KeyExtensions._

val e: Either[Try[Nothing], Nothing] = Left(Try(throw new Exception))

val inst = Guice createInjector module { binder =>
binder
.bindType[Either[Try[Nothing], Nothing]]
.toInstance(e)
} getInstance typeLiteral[Either[Try[Nothing], Nothing]].toKey
inst should equal(e)
}
}
}

0 comments on commit 7ac471b

Please sign in to comment.