question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Arrow function can be non intuitive for JS developers

See original GitHub issue

Currently y := x => x + 1 compiles to const y = x(() => x + 1) which may be a source of confusion for people coming from js which allows parenthesis to be omitted for single argument.

Should we consider adopting the js behavior by default (unless coffeeCompat is set to true) ?

Perhaps to reduce ambiguity we enforce that arg list can never be omitted, so instead of y := -> 1 we’d have to write y := () -> 1

Issue Analytics

  • State:closed
  • Created 9 months ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
STRd6commented, Dec 9, 2022

@lorefnon TypeScript doesn’t allow Type annotations on paren-less single parameters (another reason I choose to disallow them). There is definitely room to improve error messages in the LSP and I’d be happy for you to take a look.

The recommended Civet way to type annotate your example is:

z : (n: number) => number := &+1

Note the annotation is for the type of z to the left of the const assignment :=.

In cases where the ampersand function is used inline TS can usually infer the type correctly.

declare const x : number[]

y := x.map &+1

@edemaine in light of these examples I think that allowing inline type annotation of ampersand functions makes the intent a bit less clear but I’d be happy to look at some counter examples.

Also, I want to say thank you both for your contributions! It really helps me work out the practical details of the language to see how people feel most comfortable using it.

0reactions
edemainecommented, Dec 9, 2022

Would &: number + 1 or (&: number) + 1 be interesting typed shorthand for ($: number) => $ + 1?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Arrow Functions vs Traditional Functions in JavaScript - Medium
Arrow functions give you more flexibility to move from minimal to verbose where traditional JavaScript functions only have one form: verbose. Arrow functions ......
Read more >
When Not to Use Javascript Arrow Functions - Live Code Stream
An arrow function expression is a syntactically compact alternative to a regular function expression, although without its own bindings to the ...
Read more >
Arrow functions vs Regular functions in JavaScript
Arrow functions allow a developer to accomplish the same result with fewer ... Another place arrow functions make for cleaner and more intuitive...
Read more >
JavaScript Arrow Functions: How, Why, When (and ... - ZenDev
Arrow functions have a single overarching structure, and then an number of ways they can be simplified in special cases. ... A list...
Read more >
Understanding Arrow Functions in JavaScript | DigitalOcean
Arrow functions are a new way to write anonymous function expressions in JavaScript, and are similar to lambda functions in some other ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found