Marc Knaup
11/28/2020, 11:24 PMinterface SomeType
@RequiresAtLeastOneOptionalArgument
fun SomeType.with(key: String? = null, ref: Ref<*>? = null, block: () -> Unit): Unit = TODO()
fun foo(some: SomeType) {
some.with {
// useless with
}
some.with(key = "key") {
// good
}
}
Another example:
@RequiresAtLeastOneOptionalArgument
fun makeBorder(width: BorderWidth? = null, style: BorderStyle? = null, color: Color? = null): Border =
if (width != null || style != null || color != null) Border(width, style, color)
else error("At least one property must be specified.")
makeBorder()
is then statically known to be invalid.
It won’t catch makeBorder(null)
but that’s another issue.ephemient
11/28/2020, 11:44 PMprivate fun makeBorderImpl(width: BorderWidth?, style: BorderStyle?, color: Color?): Border = ...
fun makeBorder(width: BorderWidth, style: BorderStyle? = null, color: Color? = null) =
makeBorderImpl(width = width, style = style, color = color)
fun makeBorder(style: BorderStyle, color: Color? = null) =
makeBorderImpl(width = null, style = style, color = color)
fun makeBorder(color: Color) =
makeBorderImpl(width = null, style = null, color = color)
Marc Knaup
11/28/2020, 11:50 PMn
overloads but n * m
(times amount of functions). That’s a looot.
(You also wouldn’t be able to call that with three nullable variables in your example)louiscad
11/29/2020, 9:26 AM