Eric Ou
07/05/2022, 10:51 AMfun timesTwo(num: Double[(s: String)->s.toDouble(), (n: Number)->n.toDouble()]) = num * 2
Which can be separated into two methods by the compiler, and this would get rid of conversion methods, which would reduce redundancy?
Though admittedly the syntax I designed for it is pretty bad, but I trust you guys can do better than I can :D.kioba
07/05/2022, 10:59 AMfun interface DoubleConverter<A> {
fun A.toDouble(): Double
}
context(DoubleConverter<A>)
fun <A> timesTwo(num: A) : Double = num.toDouble() * 2
usage:
val context = DoubleConverter { it: String -> it.toDouble() }
with(context) {
timesTwo("3.14")
}
Eric Ou
07/05/2022, 11:01 AMkioba
07/05/2022, 11:05 AMDoubleConverter<String>
in the codebase.
@Given
object StringDoubleConverter : DoubleConverter<String> {
fun String.toDouble(): Double = it.toDouble()
}
timesTwo("3.14") // this works automatically as the compiler finds the DoubleConverter<String> in the codebase
PS: I don't exactly know where this idea is currently in the implementation but I remember there were discussions on this topic. Might be worth to check in with them 🙂raulraja
07/05/2022, 2:32 PMNiko
07/08/2022, 6:29 AM