# Please explain syntax - \[\](AsyncWebServerRequest \*request){

**URL:** <https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064>\
**Category:** Programming\
**Created:** [August 7, 2022, 12:31pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064 "2022-08-07T12:31:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [August 7, 2022, 12:31pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/1 "2022-08-07T12:31:02Z")

</div>

```cpp
    server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
        request->send(200, "text/plain", "Hello, world");
    });

```

looks like the 3rd argument may be a ptr to AsyncWebServerRequest.

i don't understand the "`[]`" preceding it?

i don't understand what looks like function body following it?

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [August 7, 2022, 12:42pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/2 "2022-08-07T12:42:00Z")

</div>

It is a [lambda expression](https://en.cppreference.com/w/cpp/language/lambda), sort of an unnamed function.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [August 7, 2022, 12:44pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/3 "2022-08-07T12:44:50Z")

</div>

can it be replaced with a more conventional function ptr?

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [August 7, 2022, 12:46pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/4 "2022-08-07T12:46:58Z")

</div>

Yes, I think so, let me check.

It seems to consume a parameter of type `ArRequestHandlerFunction`, which is defined as follows.

```cpp
typedef std::function<void(AsyncWebServerRequest *request)> ArRequestHandlerFunction;

```

So perhaps you can try to use a callback function like this:

```cpp
void callback(AsyncWebServerRequest *request) {
  request->send(200, "text/plain", "Hello, world");
}

```

---

<div class="post-metadata">

**Author:** ![johnwasser](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/johnwasser/32/214270_2.png) [@johnwasser](https://forum.arduino.cc/u/johnwasser)\
**Post date:** [August 7, 2022, 12:54pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/5 "2022-08-07T12:54:18Z")

</div>

> [@gcjr](#):
>
> ```arduino
> server.on("/", HTTP_GET, [](AsyncWebServerRequest *request){
> request->send(200, "text/plain", "Hello, world");
> });
> 
> ```

Acts like:

```arduino
void RootFolderHandler(AsyncWebServerRequest *request)
{
  request->send(200, "text/plain", "Hello, world");
}

    server.on("/", HTTP_GET, RootFolderHandler);

```

The lambda expression is handy when you need the address of very short function just to pass it to another function.

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [August 7, 2022, 1:00pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/6 "2022-08-07T13:00:18Z")

</div>

thanks

so the "`[]` " is identifying it as a lambda expression?

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [August 7, 2022, 1:04pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/7 "2022-08-07T13:04:38Z")

</div>

That is how I recognise it, but there can be something in between the brackets. This is explained in the "Lambda capture" section of the link I posted. The full syntax is also explained there (at the top of the page).

---

<div class="post-metadata">

**Author:** ![gcjr](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/gcjr/32/314015_2.png) [@gcjr](https://forum.arduino.cc/u/gcjr)\
**Post date:** [August 7, 2022, 1:08pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/8 "2022-08-07T13:08:00Z")

</div>

i saw that -- " The captures is a comma-separated list of zero or more _captures_"

i'd like to see something that describes how to use lamda expressions productively

---

<div class="post-metadata">

**Author:** ![jfjlaros](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/jfjlaros/32/716915_2.png) [@jfjlaros](https://forum.arduino.cc/u/jfjlaros)\
**Post date:** [August 7, 2022, 1:16pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/9 "2022-08-07T13:16:29Z")

</div>

> [@gcjr](#):
>
> i'd like to see something that describes how to use lamda expressions productively

Here are a couple of examples.

> **[Lambda expression in C++ - GeeksforGeeks](https://www.geeksforgeeks.org/lambda-expression-in-c/)**
>
> A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.

---

<div class="post-metadata">

**Author:** ![J-M-L](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/j-m-l/32/766245_2.png) [@J-M-L](https://forum.arduino.cc/u/J-M-L)\
**Post date:** [August 7, 2022, 2:11pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/10 "2022-08-07T14:11:39Z")

</div>

> [@gcjr](#):
>
> so the "`[]` " is identifying it as a lambda expression?

it actually tells which variables will be made available to the function when it runs (it 'captures' those variables)

A lambda can only be converted to a function pointer if it does not capture any variable.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [February 3, 2023, 2:12pm UTC](https://forum.arduino.cc/t/please-explain-syntax-asyncwebserverrequest-request/1020064/11 "2023-02-03T14:12:10Z")

</div>

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.
