Constructor lambda function


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

Thanks a lot


Should this solve more problems than it causes?

Speck has the same behavior and works without confuse the parameter and the class body (no DSL syntax).


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


Interesting idea. It would clash with normal class definitions though.

class Foo : SomeSuperClass { // is this a trailing closure or just the start of the Foo class?
    // ...


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.