dart-lang / site-www

Source for Dart website
https://dart.dev
Other
945 stars 684 forks source link

Clarify function types in `Anonymous Function` section in `Functions` documentation #5535

Open chetandr-lohono opened 6 months ago

chetandr-lohono commented 6 months ago

Page URL

https://dart.dev/language/functions/

Page source

https://github.com/dart-lang/site-www/tree/main/./src/content/language/functions.md

Describe the problem

I am not sure if anonymous functions are called Lambda or Closure, I think a Closure is a different type of function, an anonymous function can be synonymous to Lambda but not closure.

Expected fix

remove "or CLosure" from the documentation.

Additional context

No response

I would like to fix this problem.

MaryaBelanger commented 6 months ago

Hi @chetandr-lohono! "Closure" has a wide definition in common use. It includes function values in general, function values which actually close over variables in their scope, and anonymous function expressions/function literals (which create function values).

I think you're correct that lambda and anonymous function are much closer to being synonymous. But, I don't think closure is so unrelated that it needs to be removed from the anonymous functions section. A closure is a something like a more specific case of anonymous functions, or a case that makes use of anonymous functions, so can be considered a form of an anonymous function.

So where it says "...You can also create a nameless function called an anonymous function, or sometimes a lambda or closure." is not inaccurate because closures are "nameless" functions. For the intents of teaching Dart, it's safe enough to use the common understanding and group "closure" with "anonymous function". (If you want a closure example in Dart, the Isolates page has a simple one).

A similar question on stack overflow was answered by one of Dart's engineers, and provides helpful context.

The respective sections on each term's Wikipedia page are also helpful for understanding the academic definitions of how the terms overlap:

It also might be helpful to note that some other languages (like Swift and Rust) call their implementations of anonymous functions closures.

Does that help? Please let us know!