Title:
Lambda/Fat Arrow expression requires block body syntax when returning a function
| View in TrackerStatus/Resolution/Reason: Closed/Fixed/Fixed
Reporter/Name(from Bugbase): Abram A. / ()
Created: 09/26/2019
Components: Language
Versions: 2018
Failure Type:
Found In Build/Fixed In Build: 2018,0,05,315699 - hf201800-4205242.jar / CF2018U6
Priority/Frequency: Normal /
Locale/System: / Linux
Vote Count: 1
When you return a function from a Lambda/Fat Arrow function, ColdFusion requires that you use the "block body" syntax. This makes the code very cumbersome and goes against how other standard languages behave.
Here's an example (also at https://trycf.com/gist/ab323ce1270f3caeb18c8454f06f268a/acf2018):
{code:java}
<cfscript>
say = (message)=>(to)=>()=>"#message# #to#";
sayHi = say("Hi");
sayHiToAdobe = sayHi("Adobe");
writeDump( sayHiToAdobe() );
</cfscript>
{code}
Expected Results:
Hi Adobe
Actual Results:
Invalid CFML construct found error.
The above code works in Lucee and is pretty much how you'd write it in JavaScript:
JavaScript Example:
say = (message)=>(to)=>()=>`${message} ${to}`
sayHi = say("Hi");
sayHiToAdobe = sayHi("Adobe");
()=>`${message} ${to}`
sayHiToAdobe()
// "Hi Adobe"
This technique is common in functional style programming and makes composing, specializing functions, lazy evaluation and currying much more concise and easy to read and write.
Workaround:
You have to use the full function body expression syntax, which really defeats the purpose.
Example:
say = (message)=>{ return (to)=>{return ()=>"#message# #to#"}};
Attachments:
Comments: