Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a compact lambda syntax #6614

Closed
PoolloverNathan opened this issue Apr 3, 2023 · 5 comments
Closed

Add a compact lambda syntax #6614

PoolloverNathan opened this issue Apr 3, 2023 · 5 comments

Comments

@PoolloverNathan
Copy link

PoolloverNathan commented Apr 3, 2023

Describe the project you are working on

A city-building game (currently the file manager)

Describe the problem or limitation you are having in your project

The func() syntax for a lambda is slightly too verbose, especially compared to JS's () =>.

Describe the feature / enhancement and how it helps to overcome the problem or limitation

This enhancement will add additional syntax for specifying a lambda that is less verbose than the existing syntax.

Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams

# No arguments:
var no_arguments = () ->
  return 1
var no_arguments_oneline = () -> return 1
# No arguments, with implicit return:
var no_arguments_implicit = () => 2
# Arguments:
var one_arg = (a) => a * 2
var one_arg_typed = (a: float) => a * 2
# Argument list can be omitted for 0 arguments:
var no_arguments_no_list = => 2
# Possible other syntax for specifically variable definitions:
#var no_arguments_no_list_var => 2
# Parentheses can be omitted for 1 argument:
var one_arg_no_parentheses = a => a * 2
var one_arg_typed_no_parentheses = a: float => a * 2

If this enhancement will not be used often, can it be worked around with a few lines of script?

Is there a reason why this should be core and not an add-on in the asset library?

As far as I know, it is impossible to modify GDScript syntax with a script or addon.

@AThousandShips
Copy link
Member

This was already mentioned and rejected in the original proposal for lambdas #2431

@dalexeev
Copy link
Member

dalexeev commented Apr 3, 2023

vnen:

I personally don't want to add arrow functions because it would be difficult to parse (the => token appears way too late to easily disambiguate).

@Calinou Calinou changed the title Compact lambda syntax Add a compact lambda syntax Apr 3, 2023
@KoBeWi
Copy link
Member

KoBeWi commented Apr 3, 2023

() => is just 2 characters shorter than func():...
You are basically trading prefix func for suffix =>.

@dalexeev
Copy link
Member

dalexeev commented Apr 4, 2023

I would prefer func (x) => x + 1 (or fn like in PHP) since we can't use arrow functions due to parser limitations. Just removing return without any other difference is a bad idea in my opinion as it adds ambiguity.

@Calinou
Copy link
Member

Calinou commented Jun 28, 2023

Closing due to lack of support (see reactions on OP).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants