deathbeam / spoon

:ramen: Spoon is a programming language that runs blazingly fast, compiles to native code and works everywhere.
https://spoonlang.org
MIT License
56 stars 11 forks source link

Allow trailing argument in a call #21

Closed back2dos closed 8 years ago

back2dos commented 8 years ago

Looking at example code I wish that this

script.variables.set("cp", do(from : String, to : String)
  File.copy(from, to)
end)

Could be written as this:

script.variables.set("cp") do(from : String, to : String)
  File.copy(from, to)
end

While this doesn't work for arbitrary expression (without introducing weird ambiguity such as foo.bar(4)-5, which could be interpreted as foo.bar(4,-5)), it would be cool for blocks and also object literals, e.g.:

script.variables.set("env") {
  get: do(key : String)
    return Sys.getEnv(key)
  end,
  set: do(key : String, value : String)
    Sys.putEnv(key, value)
  end,
}
deathbeam commented 8 years ago

Yes this will be awesome, but a bit hard for Raxe compiler (but doable). But this is one of my goals, because it is awesome and nice looking feature :)

kobi2187 commented 8 years ago

confusing in my opinion, because you don't know what to expect from "set" method in your example. in the first version, you understand it takes a String, and a Function. The second one looks like two expressions unrelated to each other. imho, the goal should be: readable, concise and clear by glance. not just short. the programmer should be able to figure out what happens from the call site, instead of delving into the code that it leads to.

deathbeam commented 8 years ago

I already added this, but with this syntax:

script.variables.set("env") => {
  get: do(key : String)
    return Sys.getEnv(key)
  end,
  set: do(key : String, value : String)
    Sys.putEnv(key, value)
  end,
}

So with the => arrow from tink_lang.

back2dos commented 8 years ago

Aw, man, I was so hoping for first class support! :-D

Would it be hard to insert the arrow automatically, e.g. if you encounter ){?