# Command/Observer which accepts a lambda

**URL:** https://discourse.itk.org/t/command-observer-which-accepts-a-lambda/2632
**Category:** Engineering
**Created:** [January 17, 2020, 7:28pm UTC](https://discourse.itk.org/t/command-observer-which-accepts-a-lambda/2632 "2020-01-17T19:28:21Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [January 17, 2020, 7:28pm UTC](https://discourse.itk.org/t/command-observer-which-accepts-a-lambda/2632/1 "2020-01-17T19:28:21Z")

</div>

Hello,

I am wondering if anyone has already created a `itk::Command` class which accepts a C++ lambda function?

Otherwise, I am wondering if a new `itk::Object::AddObserver` method should be overloaded to accept `std::function<void()> ` so that a lambda can be directly added instead of constructing a command class.

What do you think?

---

<div class="post-metadata">

### Author: ![dzenanz](https://discourse.itk.org/user_avatar/discourse.itk.org/dzenanz/32/1093_2.png) [@dzenanz](https://discourse.itk.org/u/dzenanz)
#### Post date: [January 17, 2020, 8:02pm UTC](https://discourse.itk.org/t/command-observer-which-accepts-a-lambda/2632/2 "2020-01-17T20:02:14Z")

</div>

Sounds like a great idea!

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [March 30, 2020, 2:53pm UTC](https://discourse.itk.org/t/command-observer-which-accepts-a-lambda/2632/3 "2020-03-30T14:53:18Z")

</div>

I have implemented a similar thing in SimpleITK:

> <https://github.com/SimpleITK/SimpleITK/commit/6788d3544b100cf4b535daef0cce2756d7c0f9a0>

It is quite nice in C++, to easily be able to add a lambda function in nearly the same way as what has been done in Python.

I think we can add a similar feature to ITK in 5.2.

---

<div class="post-metadata">

### Author: ![blowekamp](https://discourse.itk.org/user_avatar/discourse.itk.org/blowekamp/32/79_2.png) [@blowekamp](https://discourse.itk.org/u/blowekamp)
#### Post date: [March 30, 2020, 10:03pm UTC](https://discourse.itk.org/t/command-observer-which-accepts-a-lambda/2632/4 "2020-03-30T22:03:26Z")

</div>

Here is an initial draft:

> <https://github.com/InsightSoftwareConsortium/ITK/pull/1735>

The original command callbacks had a signature of:

```auto
void f(const Object * caller, const EventObject & event)

```

With C++ lambdas, these are not really necessary, as those values can be specified in captures instead.

Here is the little test case of what’s implemented:

```auto
   itk::Object::Pointer o = itk::Object::New();

   int cnt = 0;
   o->AddObserver(itk::AnyEvent(), [&cnt](const itk::EventObject &) { ++cnt; });

```

I think I am in favor of removing all the arguments for the std::function/lambda use cases. What do you think?
