michaelbull / kotlin-result

A multiplatform Result monad for modelling success or failure operations.
ISC License
1.05k stars 63 forks source link

Should we offer a binding that can bind results of any Error type? #42

Closed Munzey closed 3 years ago

Munzey commented 3 years ago

i.e. should we change or offer the following as part of the api:

fun <V> binding(block: ResultBinding.() -> V): Result<V, *>

This would allow 2 things: 1) no more specifying the types when declaring a binding block:

val result = binding {
  val x = doSomething().bind()
  val y = doSomethingElse().bind()
  x + y
}

2) doSomething and doSomethingElse can have error types that dont extend from the same type. The downside of this is that now the ide will see the type of result in the above example as Result<Int, *> But is that so bad?

If we wanted to add this, would it make more sense to replace the current binding with this? or offer a second function. What would they be named? bindingAny? bindingExplicit?

michaelbull commented 3 years ago

I'm not a fan of allowing people to shoot themselves in the foot with anonymous types. If they want this behaviour it's fairly trivial for them to write their own extension function with the wildcard types themselves.