Implicit return for last expression in lambda?

Description

This works as expected (lambda returns result without needing return keyword)

writeDump([1, 2, 3].map((x) => x * 3))

...but a single expression in curly braces doesn't get returned without the keyword? why?
whether you use the return value or not, a major feature is not have to write `return`

writeDump([1, 2, 3].map((x) => {x * 3})) // maps nulls writeDump([1, 2, 3].map((x) => {return x * 3})) // maps ints

Finally, in the case of multiple expression encased in curly braces, would it not be best to implicity return the final expression (like in Kotlin)?

writeDump([1, 2, 3].map((x) => { var n = 5 x * n // this could be returned }))

https://trycf.com/gist/cda6a1384c3275ea39c747a29874433d/lucee5?theme=monokai

Environment

None

Activity

Show:

Brad Wood 14 August 2019 at 14:02

I've wondered about this as well but now that I've seen this is expected behavior in JS, I'm less interested in changing Lucee. In JS, braces mean you don't want to implicitly return anything and you will use the return keyword if you want to return something explicitly.

https://stackoverflow.com/questions/35440265/curly-brackets-in-arrow-functions

Jamie Purchase 1 March 2018 at 18:58
Edited

I'm not going to argue with the JS definition of arrow syntax but this is Lucee... my point is that the return keyword can be ommitted when calling it without braces, why should you have to write it when you use braces... you can type extra characters to your heart's content if you think that makes your code look better and I don't think for a second that including the redundant return keyword shouldn't have the same result... but while keeping an eye on the JS way of doing things, it's surely an equally smart idea to keep an eye on all other languages when developing Lucee in general? Would you honestly insist on those extra characters being required just because that's what JS says? Seems to go against the idea of the whole "lambdas are for shortened syntax" mentality if you ask me (but that's just my opinion).

Dana Barnett 19 February 2018 at 00:06

My thought was that it should behave as defined on https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions. That is to say, their example of the arrow function with a single statement w/o curly braces:

(param1, param2, …, paramN) => expression

being equivalent to

(param1, param2, …, paramN) => { return expression; }

I don't believe that there is any other form in the ES6 definition that has an implicit return. So, in your example, I wouldn't expect it to be 5, 10, 15. If you were to add the return on the x*n statement, I would.

Jamie Purchase 11 February 2018 at 06:53

Just curious about the test case added with the following;

myList = [1, 2, 3]; fn = arrayToList(myList.map((x) => { var n = 5; x * n })); expect(fn).toBe('3,6,9');

this doesn't make sense in my mind... surely we would expect fn toBe 5,10,15 ?

Jamie Purchase 11 February 2018 at 06:36
Edited

Interesting... so you would have no binding of arguments (as per JS)? Implicitly returning the last expression should not prevent you from using the return keyword on the final expression.

Details

Assignee

Reporter

Priority

New Issue warning screen

Before you create a new Issue, please post to the mailing list first https://dev.lucee.org

Once the issue has been verified, one of the Lucee team will ask you to file an issue

Created 15 December 2017 at 15:00
Updated 8 May 2020 at 18:10

Flag notifications