3

I am working in ESP8266 AsyncWebserver library and have been using this [](parameter) as an argument to some functions as shown below but does not really know what it means. Being a neophyte I am curious to know what does this convention means, its functionalities and how to use it. Hopefully experts here would help me with this.

server.on("/page",HTTP_GET,[](AsyncWebServerRequest * request){
    some code....;
    request->getParam("Param1")->value();
});
VE7JRO
  • 2,515
  • 19
  • 27
  • 29
Mr.B
  • 57
  • 1
  • 8

1 Answers1

5

That is called a lambda expression and it's a way of including the content of a function as a parameter anonymously instead of writing an actual function and using the name of it.

It's shorthand for:

void myCallback(AsyncWebServerRequest * request) {
    // some code....;
    request->getParam("Param1")->value();
}

// ...

server.on("/page", HTTP_GET, myCallback);

It does have a few benefits over traditional functions at the expense of readability, such as the ability to "capture" local variables from the surrounding scope.

Majenko
  • 105,851
  • 5
  • 82
  • 139