koka-lang / koka

Koka language compiler and interpreter
http://koka-lang.org
Other
3.16k stars 153 forks source link

Arrow syntax for anonymous functions #319

Open kuchta opened 1 year ago

kuchta commented 1 year ago

Thank you Daan and co. for a language with such a (subjectively) beautiful and (objectively) consistent and flexible syntax. One thing I don't like much though is the anonymous function syntax since it's seems odd to have keywords like fn in place where indentifiers are usually used. Since arrow syntax is already used in similar places (function types and match expressions) and people are quite used to it from other popular languages and it's minimal syntax is quite appropriate for a functional language have you ever considered it as a syntax for lambdas?

xialvjun commented 1 year ago

Zero argument anonymous function

koka:

fun printhi10()
  repeat(10) {
    println("hi")  
  }

or:

fun printhi10() {
  repeat(10) {
    println("hi")
  }
}

js with arrow syntax:

function printhi10() {
  repeat(10, () => {
    println("hi")
  })
}

I think koka's syntax is more graceful.

Not zero argument anonymous function:

koka:

fun printhi10()
  repeat(10) fn(text) {
    println(text)  
  }

or

fun printhi10() {
  repeat(10, fn(text) {
    println(text)
  })
}

js arrow syntax

function printhi10() {
  repeat(10, text => {
    println(text)
  })
}

js is good, but koka is not bad either.

By the way, I'm confused why named function in koka use fun rather than fn. Why not both of them just use the same keyword fn?

Edit in 2023/02/27: It seems fun is not bad. There are With Handlers, all are 3 characters.

with val op = <expr> 
with fun op(x){ <body> }
with ctl op(x){ <body> }
kuchta commented 1 year ago

I was thinking the same question, when I came across fun vs fn keywords 🙂

Koka is definitely not bad, it's actually much more consistent than anything I've come across and that's why I'm even bothering to ask. Thanks to Automatic braces insertion you can even omit the braces, which is great:

fun printhi10()
  repeat(10)
    println("hi")

or:

fun print10(text)
  for(1, 10) fn(i)
    println(i.show ++ ". " ++ text)

I just think the core language is better off avoiding keywords as much as possible since it takes up the name space and adds noise, but that's probably subjective...

TimWhiting commented 6 months ago

One thing to note is that typically using keywords helps to disambiguate syntax earlier which means it can parse files much quicker. This is something that most newer languages try to do.