clio icon indicating copy to clipboard operation
clio copied to clipboard

Ambiguity from nested anonymous functions

Open Merlin04 opened this issue 2 years ago • 3 comments

Is your feature request related to a problem? Please describe. I came across Clio and it looks really cool, I'm excited to try using it for some projects! One issue I ran into is that in nested anonymous functions I can't figure out how to specify which parameters belong to which function.

For example, here's a function in JavaScript:

const thing = (m) => [...Array(5).keys()].map(item => item * m).forEach(item => console.log(item));

I'm not sure how I'd write this in Clio due to the nested anonymous functions creating ambiguity (I can't specify that item is a parameter of the inner function and m is a parameter of the outer function):

thing = (0..5 -> .toArray -> * (@item * @m) -> console.log)

Describe the solution you'd like I'm not sure how this could be resolved while keeping Clio's implicit parameter syntax. Maybe there could be some sort of new syntax like JavaScript's function() {} that keeps the fn and explicit arguments but still returns an anonymous function? Something like:

thing = fn m: (0..5 -> .toArray -> * (@item * m) -> console.log)

Describe alternatives you've considered I could just write thing as a regular function:

fn thing m:
  0..5 -> .toArray -> * (@item * m) -> console.log

But this doesn't return an anonymous function so it wouldn't be a valid workaround in all cases.

Merlin04 avatar Oct 09 '21 17:10 Merlin04

The anonymous functions need some work, I'll address this issue in the next release. The implicit anonymous functions are useful in cases like

arr -> .filter (@it % 2)

so I'd like to try and keep that syntax, but yes, your argument is valid and the issue should be resolved.

pouya-eghbali avatar Oct 10 '21 17:10 pouya-eghbali

Personally, I'm not a fan of long anonymous functions, looking at a function it should be obvious what the function does.

A good read on this topic can be found here: https://hackernoon.com/three-reasons-i-avoid-anonymous-js-functions-like-the-plague-7f985c27a006

pouya-eghbali avatar Oct 10 '21 17:10 pouya-eghbali

@Merlin04 for now, this works:

https://playground.clio-lang.org/?code=thing%20%3D%20(0..5%20-%3E%20.toArray%20-%3E%20*%20(%40item%20*%20%40m)%20%40m%20-%3E%20console.log)%0A%0Aexport%20fn%20main%3A%0A%20%20thing%2010%0A%20%20thing%2020

it passes the outer param to the inner anonymous function

pouya-eghbali avatar Oct 11 '21 18:10 pouya-eghbali