3 ms·
Is that really that much better than C blocks? #include <stdio.h> int main (int argc, char const *argv[]) { int (^add)(int, int) = ^(int x, int
by gok 15y ago
Is that really that much better than C blocks?
#include <stdio.h>
int main (int argc, char const *argv[]) {
int (^add)(int, int) = ^(int x, int y) {
return x + y;
};
int (^max)(int, int) = ^(int x, int y) {
return (x > y) ? x : y;
};
printf("block: add:%d\n", add(2,3));
printf("block: max:%d\n", max(2,3));
return 0;
}
- juiceandjuice 15y agoI personally think so, as I think blocks in C are a clumsy way around the issue (probably because C doesn't natively have garbage collection) and it feels natural in go, but I know others will disagree. Also, I don't think you can make it anonymous, can you? func(x int, y int){fmt.Printf("%d\n",x+y)}(2,4)
- gok 15y agoYou sure can. ^(int x, int y){printf("%d\n",x+y);}(2,4); Automatic memory management is interesting, but orthogonal.