https://kotlinlang.org logo
#announcements
Title
# announcements
d

dimsuz

07/10/2020, 9:35 AM
I have a simply looking composition with generics and have even the new type inference fail on it. Is this a bug?
Copy code
interface Validator<T> {
  fun validate(input: T)
  fun and(other: Validator<T>): Validator<T> = TODO()
}

fun <T> andExternal(v1: Validator<T>, v2: Validator<T>): Validator<T> = TODO()

fun <T> isNotEmpty(): Validator<T> = TODO()
fun <T> isNotBlank(): Validator<T> = TODO()

fun doStuff(validator: Validator<String>) { }

fun main() {
  // isNotEmpty is highlighted: "not enough information to infer type variable T"
  doStuff(isNotEmpty().and(isNotBlank()))
  // no error when using external variant of "and", inferred as String!
  doStuff(andExternal(isNotEmpty(), isNotBlank()))
}
f

flosch

07/10/2020, 10:03 AM
It's not an error, you never specify the type for your
isNotEmpty
and other functions
d

dimsuz

07/10/2020, 11:11 AM
I do. By specifying the explicit type in
doStuff
. Also note, that line with
andExternal
has no error, while the only difference from
and
(which has an error) is being declared out of the class with
this
made into an explicit parameter.
f

flosch

07/10/2020, 1:27 PM
Ah yea I oversaw that. I cannot tell you why but
doStuff(isNotEmpty<String>().and(isNotBlank()))
works. Also this works:
Copy code
fun test(list: List<String>) {}
test(listOf())
d

dimsuz

07/10/2020, 1:57 PM
I duplicated this question in #arrow channel and got a detailed answer there, in case you interested. Thanks for response!
👍 1