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

arrow functions or lambda functions support #321

Closed
blueway opened this issue Jun 24, 2020 · 8 comments
Closed

arrow functions or lambda functions support #321

blueway opened this issue Jun 24, 2020 · 8 comments

Comments

@blueway
Copy link

blueway commented Jun 24, 2020

like es arrow function (x,y) => x+y or like python lambda function g = lambda x:x+1

@xushiwei
Copy link
Member

Thank you for your proposal. We will consider it seriously.

@xushiwei xushiwei added feature and removed proposal labels Jul 23, 2020
@xushiwei
Copy link
Member

xushiwei commented Jul 23, 2020

Define lambda functions

f := (arg1 ArgType1, arg2 ArgType2, ..., argN ArgTypeN) => expr(arg1, arg2, ..., argN)

g := (arg1 ArgType1, arg2 ArgType2, ..., argN ArgTypeN) => {
    ...
    return expr(arg1, arg2, ..., argN)
} 

Note: output parameters are always omitted.

Passing lambda functions as function parameters

foo.Bar(arg => expr(arg))
foo.Bar((arg1, arg2, ..., argN) => expr(arg1, arg2, ..., argN))
foo.Bar((arg1, arg2, ..., argN) => {
    ...
    return expr(arg1, arg2, ..., argN)
})

Types of input parameters can be omitted if they can be automatically deduced.

For example, in Go we can write code as following:

foo.Map(func(x float64) float64 { return x*x })

And in Go+ it can be:

foo.Map(x => x*x)

@damonchen
Copy link
Contributor

If we write the code below:

m := 100
cc := (arg1, arg2, arg3) => {
    m := 10

    dd := (d1, d2, d3) => {
        println(m);
    }
}

what is the print? or compile error?

@mlboy
Copy link

mlboy commented Jul 23, 2020

I'd like ES6 style

@xushiwei
Copy link
Member

If we write the code below:

m := 100
cc := (arg1, arg2, arg3) => {
    m := 10

    dd := (d1, d2, d3) => {
        println(m);
    }
}

what is the print? or compile error?

Will compile fail because types of input parameters are not specified. If we do this, its output is "10\n".

@xushiwei
Copy link
Member

xushiwei commented Jul 26, 2020

Define lambda functions

f := (arg1 ArgType1, arg2 ArgType2, ..., argN ArgTypeN) => expr(arg1, arg2, ..., argN)

g := (arg1 ArgType1, arg2 ArgType2, ..., argN ArgTypeN) => {
    ...
    return expr(arg1, arg2, ..., argN)
} 

Note: output parameters are always omitted.

To make the following code

foo.Map(x => x*x)

equals to

f := x => x*x
foo.Map(f)

We automatically deduce the prototype of lambda f till first time it is used.

So, Defining lambda functions will be same as passing them as function parameters.

f1 := arg => expr(arg)
f2 := arg => {
    ...
    return expr(arg)
}
g1 := (arg1, arg2, ..., argN) => expr(arg1, arg2, ..., argN)
g2 := (arg1, arg2, ..., argN) => {
    ...
    return expr(arg1, arg2, ..., argN)
}

@shendiaomo
Copy link

x => x*x

How to write a lambda without parameters and multiple return values?

f1 := => expr1, expr2

or

f1 := () => (expr1, expr2)

@xushiwei
Copy link
Member

xushiwei commented Jul 31, 2020

How to write a lambda without parameters and multiple return values?

f1 := => expr1, expr2

or

f1 := () => (expr1, expr2)

Will be:

f1 := => (expr1, expr2)  // or:
f1 := () => (expr1, expr2)

@xushiwei xushiwei added this to the Go+ v1.0 milestone Aug 10, 2021
xushiwei added a commit that referenced this issue Aug 11, 2021
xushiwei added a commit that referenced this issue Aug 11, 2021
xushiwei added a commit that referenced this issue Aug 11, 2021
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