arve
11/26/2024, 1:30 PMList<T:Any?>
or a T:Any?
, with slightly different implementations. The goal is for the caller not to have to care whether the param is a value or collection of values (SQL interpolation stuff)
What’s a good way to allow these to have the same name?
currently I have
inline fun <reified T : Any> Statement.helperList(param: Collection<T>?, stmt: (Param<*>) -> Statement) = TODO("thing that has to know T")
inline fun <reified T : Any> Statement.helper(param: T?, stmt: (Params<*>) -> Statement) = TODO("Same thing but handled a differnt way since its not a collection"
Currently if I give them the same name, helper
will be called even if the argument is a collection, because it also matches Any
. Is there a way to constrain T so that it’s “Not collection” somehow?Joffrey
11/26/2024, 1:50 PMAny
, the overload with List
should be chosen firstarve
11/26/2024, 1:58 PMarve
11/26/2024, 1:59 PMarve
11/26/2024, 2:01 PMstmt
lambda signature at call site, based on param
’s type. Which i suppose would be unreasonable to expect anyway 😛arve
11/26/2024, 2:03 PMT: Comparable<T>
and T: Collection<T>
it’s able to resolve. But this again would require us to implement Comparable on various objects, just for this useage 🤔Joffrey
11/26/2024, 3:16 PMDaniel Pitts
11/26/2024, 6:31 PMDaniel Pitts
11/26/2024, 6:36 PMdata object Bar
data object A
data object B
inline fun <reified T : Any> Bar.foo(items: Iterable<T?>): ((A) -> Any) -> Any = { it(A) }
inline fun <reified T : Any> Bar.foo(item: T?): ((B) -> Any) -> Any = { it(B) }
fun main() {
println(Bar.foo(listOf(1, 2, 3))({ it }))
println(Bar.foo(1)({ it }))
}
Daniel Pitts
11/26/2024, 6:47 PMDaniel Pitts
11/26/2024, 6:47 PMdata object A
data object B
class Curried<T, R>(val onNext: (T) -> R) {
infix fun handle(next: T) = onNext(next)
}
inline fun <reified T : Any> foo(items: Iterable<T>) = Curried<(A) -> Any, Any> { it(A) }
inline fun <reified T : Any> foo(item: T) = Curried<(B) -> Any, Any> { it(B) }
fun main() {
foo(listOf(1, 2, 3)) handle { println("$it for a list") }
foo(1) handle { println("$it for a single item") }
}
Ronny Bräunlich
11/27/2024, 5:54 AMinline fun <reified T : Any> Statement.helper(vararg param: T?, stmt: (Params<*>) -> Statement)
you can then call the method like this
Statement("bla").helper(Params("ABC"), Params("123")) {...}
Statement("bla").helper(listOf(Params("ABC"), Params("123"))) {...}