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 multiline method chain syntax #393

Open
mtshiba opened this issue Feb 10, 2023 · 4 comments
Open

Add multiline method chain syntax #393

mtshiba opened this issue Feb 10, 2023 · 4 comments
Labels
enhancement New feature or request

Comments

@mtshiba
Copy link
Member

mtshiba commented Feb 10, 2023

This syntax allows

x.foo().bar().baz(y)

to be rewritten as

x
    .foo()
    .bar()
    .baz(y)

Indentation is important,

x
.foo()

is invalid (this means sequential execution of x and .foo()).

@mtshiba mtshiba added the enhancement New feature or request label Feb 10, 2023
@zetashift
Copy link

How about making this work with |> instead? Makes things a bit more "functional pipeline"-ey to use.

x
    |> (_.foo())
    |> (_.bar())
    |> (_.baz(y))

I guess it sacrifices readability a lot more...
Also see fsharp/fslang-suggestions#506 for a similiar discussion

@mtshiba
Copy link
Member Author

mtshiba commented Feb 11, 2023

The pipeline operator already exists in Erg.

True |> assert()
assert -1 + -1 |> .abs() == 2

It might be a good to add the rule regarding this operator.

@zetashift
Copy link

Oh cool! Ignore me then :P

@bmitc
Copy link

bmitc commented Mar 3, 2023

@zetashift This isn't entirely related to Erg, but for what it's worth since this issue was linked to an F# issue, F# actually allows method chaining.

type Test() =
    member this.A () = this
    member this.B () = this
    member _.C () = ()

let test = Test()

test.A()
    .B()
    .C()

// or

test.A().B().C()

The linked feature of allowing _ is to support other different use cases and probably wouldn't be recommended just for method chaining using |>.

While strange indentation like

test
    .A()
    .B()
    .C()

is allowed in F#, it isn't recommended. That might be relevant to the original proposal here for Erg.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants