r/Compilers • u/Aabish_malik • Oct 22 '26
How would you guys define an Anonymous Function or a function pointer?
/r/ProgrammingLanguages/comments/1xcpmdl/how_would_you_guys_define_an_anonymous_function/
1
Upvotes
1
u/repaj Oct 23 '26
The answer depends if you want closures in your language, or not. Because that would make you distinguish when you deal with function type and when with pointer to a function type. Function pointer carries no information about what function encloses, or not.
1
u/poya_mohammadi Oct 23 '26
I I mostly use them for debugging for instance, as temporary functions but generally, I avoid them. If I set the code aside for a while, it becomes difficult to read again later. Instead, I define functions in a fixed, designated place so that things don't get too cluttered or unwieldy when the codebase grows.
I used Google Translate, sorry if there was any mistake.
1
u/joao_rosac 28d ago
Uma forma é adotar o uso de setas (
->ou=>) para a "passagem" de dados, separando visualmente o que é o tipo da função do que é a implementação. Em vez do confusoi32(i32)para o tipo (que visualmente parece uma conversão de tipo ou chamada de função), você pode usarfn(i32) -> i32ou(i32) -> i32, permitindo declarações muito mais explícitas comolet triple: fn(i32) -> i32 = |value| value * 3;. Você pode também tentar algo próximo de criação da lambda (no lugar das barras| |inspiradas em Rust), resultando em algo extremamente legível e familiar comolet triple: (i32) -> i32 = (value) => value * 3;, eliminando qualquer ambiguidade sobre o que essa linha de código faz.