I have a simply looking composition with generics ...
# announcements
d
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
It's not an error, you never specify the type for your
isNotEmpty
and other functions
d
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
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
I duplicated this question in #arrow channel and got a detailed answer there, in case you interested. Thanks for response!
👍 1