wren-lang / wren

The Wren Programming Language. Wren is a small, fast, class-based concurrent scripting language.
http://wren.io
MIT License
6.86k stars 550 forks source link

[Feature] Function with receiver #1048

Open antonbashir opened 3 years ago

antonbashir commented 3 years ago

sorry for my strange language :((

Hello everyone! First I would like to thank developers for this brilliant language!

This issue is about adding some functional looks like "function literals with receiver" in Kotlin

Some words about terminology. When I say receiver I mean the owner of current function scope. If we are in class method scope then receiver is this .

For example instead of current: [myObject1, myObjec2].where {|element| element.myValue > 0} will be smth like: [myObject1, myObjec2].where { myValue > 0 }.

Function inside eachFile "knows" its receiver parameter of "MyClass" type and expression myValue > 0 will be interpreted like |myObject| myObject.value > 0.

I suppose that such "receiver" functions will substantially reduce size of code. Just compare size of next code strings when using some custom CLI for dealing with simple file tasks:

current: my-cli> files.eachFile { |file| file.content.replace("str1", "str2") } could be: my-cli> files.eachFile { content.replace("str1", "str2") }

There are could be two methods for distinguish functions with receiver and without it:

  1. current - call(parameters...)
  2. additional -receiverCall(receiver, parameters...)
PureFox48 commented 3 years ago

It's been a while since I last used Kotlin but I had wondered whether there might be a case for including something like their implicit it variable when a lambda expression just takes a single parameter in Wren?

However, I decided that it wouldn't be worth the hassle even if it could be implemented within Wren's single pass compiler. After all, it's not exactly verbose to have to include |it| in your functions as we do now.

Similarly, I don't think their Function literals with receiver would be a good fit for Wren. There is a strong desire to keep the language simple and such a feature would be anything but.