BMG
10/11/2018, 8:10 AMinline fun <reified T> Config.xAxis(block: T.() -> Unit, axisType: AxisType) {
xAxis = when (T::class.java) {
NumericAxis::class.java -> {
NumericAxis().apply(block)
}
else -> throw UnsupportedOperationException("currently unsupported")
}
}
Why does it not compile? Any thoughts?marstran
10/11/2018, 8:11 AMxAxis
?arekolek
10/11/2018, 8:23 AMxAxis = when (T::class) {
NumericAxis::class -> NumericAxis().also { block(it as T) }
else -> throw UnsupportedOperationException("currently unsupported")
}
BMG
10/11/2018, 8:25 AMxAxis
is a DSL function. Usage is like,
xAxis<NumericAxis> {
}
block
does not take any parameter. It has receiver type T
which comes from DSL usage. We did,
fun <T> Config.xAxis(block: T.() -> Unit, axisType: AxisType) {
xAxis = when (axisType) {
AxisType.NUMERIC -> NumericAxis().apply(block as NumericAxis.() -> Unit)
else -> throw UnsupportedOperationException("currently unsupported")
}
}
But looking to avoid casting.marstran
10/11/2018, 8:32 AMxAxis = when(axisType) { .... }
. You can't assign anything to a function.arekolek
10/11/2018, 8:32 AMConfig
gergo
10/11/2018, 8:32 AMBMG
10/11/2018, 8:34 AMxAxis
inside that function is a property of type Axis
.arekolek
10/11/2018, 9:00 AMblock
having a receiver instead of parameter, see my related question here https://kotlinlang.slack.com/archives/C0B8MA7FA/p1504173358000320?thread_ts=1504173358.000320BMG
10/11/2018, 9:19 AMSam
10/11/2018, 2:52 PMinline fun <reified T : ViewGroup.LayoutParams> View.updateLayoutParams(block: T.() -> Unit) {
val params = layoutParams as T
block(params)
layoutParams = params
}