logoalt Hacker News

tancoptoday at 7:31 PM1 replyview on HN

My ideal syntax for function pointers is:

  fn signal(signum: int, fp: fn(int -> void)) -> fn(int -> void)
It keeps the parameter and return types inside, makes it obvious that it's a function using the fn keyword (or func or whatever), short and readable.

When you add more parameters you get `fn(int, char -> int)`. That's the only sane way to handle it and it also supports multiple return values if you want them.

Bonus: all type modifiers should be prefix like `?*MyStruct` and control flow should be postfix `task.await.match { ... }`. Every language should either have a pipe operator or let you call any function with method syntax. `x |> f |> g` is better than `g(f(x))`.


Replies

bpavuktoday at 8:54 PM

> My ideal syntax for function pointers is

this is actually very Kotlin, in Kotlin it'd be `val lambda: (Int, Char) -> Int = { myInt, myChar -> return 69 }`

Zig has also come very close:

  const Call2Op = *const fn (a: i8, b: i8) i8;
  fn doOp(fnCall: Call2Op, op1: i8, op2: i8) i8 {
      return fnCall(op1, op2);
  }
> Every language should either have a pipe operator or let you call any function with method syntax

there is a nicety in Zig that may help: `fn1(p1, ...)` is the same as `fn1.p1()`, so `fn2(fn1(p1))` can be unfolded into `p1.fn1().fn2()`