1

Is there way to create a factory binding, which can produce null?

For example,

bind<String?> with factory { x: Int -> 
    when (x) {
        1 -> "A"
        2 -> "B"
        else -> null
    }
}

Unfortunately, bind<String?> gives error.

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
Alexey
  • 2,980
  • 4
  • 30
  • 53

1 Answers1

3

End up with Optional:

bind<Optional<String>> with factory { x: Int -> 
    when (x) {
        1 -> Optional.of<String>("A")
        2 -> Optional.of<String>("B")
        else -> Optional.empty()
    }
}
Alexey
  • 2,980
  • 4
  • 30
  • 53