r/ProgrammingLanguages • u/Aabish_malik • Oct 22 '26
How would you guys define an Anonymous Function or a function pointer?
Me and a few of my friends have been working on a programming language that we initially started for a compiler design class for some time and recently started working on support for anonymous functions, lambdas and function pointers to make callbacks easier to write.
Currently Captures on variables outside the function scope are detected and captures without complete context moving.
I'm a bit skeptical or the syntax that we're currently deciding on (its not that clear on first view) , so i'm looking for reviews and maybe some ideas.
I'll just include some examples we have right now.
let double = |i32 value| value * 2;
i32(i32) triple = |value| value * 3; // i dont like this rn, not that clear on
// first look that this is a lambda
let absolute = |i32 value| {
if (value < 0) { return -value; }
return value;
};
let classify = |i32 value| match value {
0: "zero",
_: "nonzero",
};
17
u/NullOfSpace Oct 22 '26
What do your regular functions look like? The syntax should generally be a stripped-down version of that.
6
u/KittenPowerLord Oct 22 '26
I myself am inspired by the Haskell way:
I'm using curly braces to indicate various implicit/inferred stuff, so like capturing (although it's to be decided whether default capturing is by pointer or by value, but you can specify it anyways). `@` indicates pointery stuff
3
u/TartOk3387 Oct 22 '26
Note: a function pointer is NOT the sane as a higher order function and/or closure, since function pointers don't capture the environment.
4
u/WittyStick Oct 22 '26 edited Oct 22 '26
Where
->is right associative. (Or=>if->is already taken - has precedence in eg, c#).Is a function returning a function. It's parsed as
foo -> (bar -> baz).->should have lower precedence than any other expression operator, but higher precedence than=for binding.Parsed as
Application should be left associative.
Parsed as:
3
u/phischu Effekt Oct 23 '26
We like
{ (i32 value) => value * 2 }and others dofun(i32 value) { value * 2 }.1
u/LazyUntilYouNeedMe Oct 22 '26
Maybe `lambda (i32 a) {a + 1}`. Lambda is quite the standard name for anonymous functions.
4
u/Key_River7180 Nain. Oct 22 '26
Me, I'd do λ [comma-separated args] . [body] akin to lambda calculus. I just really like it. You can use
lambdinstead of the λ Unicode symbol too.2
u/Lovely_Cygnus Oct 22 '26 edited Oct 22 '26
just look at λ-calculus as it is implemented in LISP. the functions are always unnamed lambdas, it’s about you to give them a “name“ using (define …) or not.
just to let you understand:
(lambda (num) (+ num 1)) is a function, that does the obvious…
(define yourname (lambda …)) is the link between the name yourname and the lambda function.
you can use it without a name, as in ( (lambda (num) (+ num 1)) 5) that returns 6, or
use the named version, as in (yourname 5) that returns, obviously, 6.
2
u/fdwr Oct 23 '26
🤔 I always found those languages that used
|to be harder to visually parse because the opening|and the closing|(or was that the opening|and the closing|? :b) look exactly the same, and are not proper opening/closing pairs - see Bidi Paired Brackets for a nice list.Like u/NullOfSpace says, whatever your normal functions look like, there should be some coherency with them. Subjectively, I always appreciated the balanced conciseness and clarity of
foo(x => x * 2)andfoo((a, b) => sqrt(a * a + b * b)).3
u/editor_of_the_beast Oct 22 '26
A function without a name.
2
u/initial-algebra Oct 22 '26 edited Oct 22 '26
The only capture syntax you need, e.g. to distinguish between capture-by-value and -by-reference, is let-insertion, or
genlet. I'm not 100% sure you even need this, but I will use any chance I get to promote it, because it's very cool.becomes
No need for
movelambdas (it becomes the default) or C++ captures. For nested lambdas, you can support multi-level or labelledgenlet:becomes
I'm not suggesting you have to use this concrete syntax. I personally am not a big fan of Ruby/Rust-style lambdas with
|...|, anyway, and you certainly don't need to use Rust-lifetime-style labels. You also don't need to call itgenlet, which comes from metaprogramming.1
u/Background_Class_558 Oct 22 '26
it's pretty good
1
u/SnappGamez Rouge Oct 22 '26
```
val absolute := (value: int32) do if value < 0 then
-value
else
value
end
```
1
u/criloz tagkyon Oct 22 '26
Lately I have been liking this pattern a lot.
1
u/david-1-1 Oct 24 '26
An anonymous function is used when a function is to be applied to various arguments (or within a fixed scope) to provide elegance and to reduce what otherwise might be a confusion of many function names. You can use a named function instead, if you prefer. It won't hurt.
1
u/Turbulent_Humor_3213 Oct 26 '26
I'd keep anonymous functions as simple as possible syntax-wise, especially if readability is one of your concerns.
Something like
(x, y) => x + yfeels pretty intuitive to me.I'd also make a clear distinction between function pointers and closures. A function pointer just refers to a function, while a closure can also capture variables from its surrounding scope.
For captures, I'd probably start with automatic capture and only introduce explicit capture syntax if there's a good reason for it.
1
u/Current-Scheme-9896 4d ago
I think supporting anonymous functions and closures is a very useful feature. One important aspect is how variables from the surrounding scope are captured
For example, a lambda function might use a variable defined outside its body. In this case the function needs to preserve access to that variable even when it is called later
I also think its important to distinguish closures from function pointers. A closure can contain both the function code and the captured environment, while a simple function pointer only refers to the function itself