Alexey
Alexey

Reputation: 3192

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

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.

Upvotes: 1

Views: 486

Answers (1)

Alexey
Alexey

Reputation: 3192

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()
    }
}

Upvotes: 3

Related Questions