# Constructor lambda function

**URL:** <https://discuss.kotlinlang.org/t/constructor-lambda-function/8330>\
**Category:** Language Design\
**Created:** [June 29, 2018, 9:22am UTC](https://discuss.kotlinlang.org/t/constructor-lambda-function/8330 "2018-06-29T09:22:49Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![smallufo](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/smallufo/32/1618_2.png) [@smallufo](https://discuss.kotlinlang.org/u/smallufo)\
**Post date:** [June 29, 2018, 9:22am UTC](https://discuss.kotlinlang.org/t/constructor-lambda-function/8330/1 "2018-06-29T09:22:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [June 29, 2018, 10:39am UTC](https://discuss.kotlinlang.org/t/constructor-lambda-function/8330/2 "2018-06-29T10:39:33Z")

</div>

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).

---

<div class="post-metadata">

**Author:** ![Beholder](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/beholder/32/2078_2.png) [@Beholder](https://discuss.kotlinlang.org/u/Beholder)\
**Post date:** [July 3, 2018, 1:01pm UTC](https://discuss.kotlinlang.org/t/constructor-lambda-function/8330/3 "2018-07-03T13:01:56Z")

</div>

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:

```auto
val pattern1 = Pattern("pattern 1") { it: Model ->
    // DSL-like
}

```

---

<div class="post-metadata">

**Author:** ![arocnies](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/arocnies/32/5143_2.png) [@arocnies](https://discuss.kotlinlang.org/u/arocnies)\
**Post date:** [July 3, 2018, 5:08pm UTC](https://discuss.kotlinlang.org/t/constructor-lambda-function/8330/4 "2018-07-03T17:08:38Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![pdvrieze](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/pdvrieze/32/1882_2.png) [@pdvrieze](https://discuss.kotlinlang.org/u/pdvrieze)\
**Post date:** [July 6, 2018, 8:25am UTC](https://discuss.kotlinlang.org/t/constructor-lambda-function/8330/5 "2018-07-06T08:25:47Z")

</div>

> [@arocnies](#):
>
> Interesting idea. It would clash with normal class definitions though

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.
