[Dlang-internal] [OT] What do you think about declaring functions with lambda syntax?
ddcovery
antoniocabreraperez at gmail.com
Sat Dec 5 03:51:24 UTC 2020
On Saturday, 5 December 2020 at 00:55:23 UTC, IGotD- wrote:
>
> It is really ugly and doesn't help readability. It looks like
> another language.
For me it is absolutely wonderful and totally readable
(expressive)... this is the way I like to write code when
possible and this is, basically, D.
Yesterday, I found myself writing this:
ulong factorial(ulong n){return
n>1 ? n*factorial(n-1) : 1
;}
Lambda notation is "syntax sugar" that compiler can transform to
"{return ... }" transparently
You choose your favorite one for each situation (like f(x) or
x.f() or x.f, or like named parameters that will be introduced
in future versions of D)
Dart already offers this capability.
// named function c like notation
int factorial(int n) { return ...; }
// named function lambda notation
int factorial(int n) => ...;
// Anonymous function c like notation
final int Function(int) f = (n){ return n*n; }
// Anonymous function lambda notation
final int Function(int n) f = (n) => n*n;
Scala does something similar (not really lambda notation, only
the possibility to remove brackets because "return" is implicit)
def factorial(n:Int):Int = if (n>1) n * factorial(n-1) else 1;
def factorial(n:Int):Int = { if (n>1) n * factorial(n-1) else 1; }
In typescript and python you can assign a lambda to a variable
and refer it from the body
// Typescript
const factorial = (n:bigint)=> n>1? n*factorial(n-1):1;
// Python
factorial = lambda n : factorial(n-1)*n if n>1 else 1;
This is not the est solution, but this is something (simple and
effective)
As DConf2020 exposed "D, the functional programming language
nobody is talking about". In my opinion, the possibility of
functions without brackets/return would be a good reinforcement.
More information about the Dlang-internal
mailing list