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

GDScript: lambdas #16090

Closed
wants to merge 1 commit into from
Closed

Conversation

poke1024
Copy link
Contributor

A proposal for lambdas.

Two supported syntax variants:

(1) short form:

var f = func x: x + 1

(2) long form:

var f = func x:
    return x + 1

Note that return is implicit with the short form.

Captures are explicit. I propose using []:

var f = func x [a, b]:
    return x + a + b

There will be discussion about this. Here's my arguments for having explicit captures:

  • Getting implicit captures right means breaking various assumptions in the compiler regarding compile orders and dependencies (it's a probably disruptive change)
  • Being less magic and more explicit might be more in the general philosophy of GDScript (compare GDScript: unpacking of arrays using matcher-like syntax #15561 vs GDScript: unpacking of arrays using Python-like syntax #15216)
  • With GDScript being a ref-counted, non-gc language, and JS and Python people jumping onto lambdas, automatic captures might do more harm than good. With explicit lambdas it might be much cleaner to separate strong from weak references, as you are encouraged to think about the type of each single capture.

Together with #16089, this PR could introduce a syntax for weakrefs that is fully transparent:

var f = func x [a, weakref b]:
    a.some_method() # a is strong
    if b: # b is weak
        b.my_method()

Further notes:

@groud
Copy link
Member

groud commented Jan 26, 2018

Related #12170

@ghost
Copy link

ghost commented Jan 26, 2018

Also #2704

@reduz
Copy link
Member

reduz commented May 7, 2018

@vnen is doing a GDScript overhaul to add optional typing. Afterwards it should be easier to see how to do lambdas.

@vnen
Copy link
Member

vnen commented May 7, 2018

I would probably be less conservative with this by adding a new type of node in the parser and doing the needed changes in the compiler (and bytecode, if need be). But yeah, lemme finish my optional typing proposal first.

@akien-mga
Copy link
Member

For of all thanks for working on this and for bringing a working proposal for lambda support in GDScript.

As we discussed together in our IRC meeting on June 20 (http://godot.eska.me/irc-logs/devel/2018-06-20.log from 21:10), this should be added in Godot 3.2 following the GDScript roadmap that we agreed upon: godotengine/godot-roadmap#23.

So I'll close this PR for now, and it will likely be used as reference for finally implementing and merging lambdas in GDScript after the other prerequisites have been done (and gladly by you or with your help :)).

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

Successfully merging this pull request may close these issues.

5 participants