Hi there. I have a general question about generics...
# getting-started
j
Hi there. I have a general question about generics. Suppose I have input data in form of lines, each matching some regex that could be then mapped to a data class or whatever. But on each day it will be different format, different regex and data. YES it’s #advent-of-code time again 🙂 I created an extension function to help me deal with it:
Copy code
fun <R> String.parseRecords(regex: Regex, op: (MatchResult) -> R): Sequence<R> =
    lineSequence().filterNot(String::isBlank)
        .map { regex.matchEntire(it) ?: error("WTF `$it`") }
        .map(op)
That works just fine. How would it look like if I want to make parameters optional (with some default mapping)? I can use
regex: Regex = "(.*)"
but the
op: (MatchResult) -> R = { it.destructured.component1().toInt()}
is a big no-no, because of Type mismatch: inferred type is Int but R was expected. Is it even possible to add default parameter values for generic functions?
s
Sounds like a similar question to this
j
Thank you! Yesss, that what I thought (I went with overload also, but I wondered if there’s more kotlinish way to do that 🙂 )