I know it is possible to have function with last lambda receiver , that makes calling function DSL-like .
But is it possible to make it available in constructor ?
such as :
sealed class Pattern(val name: String, predicate: (Model) -> Boolean)
class Pattern1 : Pattern("pattern 1", { it: Model ->
// not DSL-like
})
Is it possible to achieve this (which makes it more DSL-like) :
class Pattern1 : Pattern("pattern 1") { it: Model ->
// DSL-like
}
In the example above, there is no need really to create new class, because this “class” doesn’t add new or override any methods or properties. You can write:
val pattern1 = Pattern("pattern 1") { it: Model ->
// DSL-like
}
It wouldn’t actually conflict as a definition has a colon. This is a constructor call. I believe it is legal, but even if it isn’t you could either use a factory function (with the class name) or an invoke operator to make it work. The question is whether it is a good idea, but it is possible.