Stefan Beyer
11/19/2021, 1:59 PMA
expands to Any
and everything is fine...
--------------
Original question:
I just fiddled around with some DSL stuff and found some weird behavior.
Consider these two files. One is inside a library, defining some DSL:
package some.library.dsl
class Builder {
fun <A> foo(first: A, second: A) { println("MEMBER called with $first and $second") }
}
fun build(config: Builder.()->Unit) = Builder().apply(config)
And one is inside a consuming app:
package <http://my.app|my.app>
import some.library.dsl.Builder
import some.library.dsl.build
fun <A, B> Builder.foo(first: A, second: B) { println("EXTENSION called with $first and $second") }
fun main() {
build {
foo(1, 2)
foo("hello", 0.4) // ???
}
}
I expected a compile error at the line marked with // ???
, but the code compiles, runs and prints the following:
MEMBER called with 1 and 2
MEMBER called with hello and 0.4Is this expected behavior? As I see it, this could break existing dsl libraries, as they could receive arguments of unexpected types, right?