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

Indentation of curried functions #957

Open
ghivert opened this issue Oct 8, 2020 · 4 comments
Open

Indentation of curried functions #957

ghivert opened this issue Oct 8, 2020 · 4 comments

Comments

@ghivert
Copy link

ghivert commented Oct 8, 2020

Hi !

I really like the formatter, but there’s some cases where I wonder if it’s by design or not.
I wonder mainly about curried functions. I’m coming mainly from functional programming (Haskell and stuff) and it’s usual to have something like this:

// In JavaScript for example
const fun = arg1 => arg2 => arg3 => {
  // body...
}
// In Dart for example
fun(arg1) => (arg2) => (arg3) {
  // body...
};

But when doing the latter, it’s indented like this:

fun(arg1) => (arg2) => (arg3) {
    final var = 1;
    return var;
  };

It feels like there’s two unneeded spaces in the function body. With indent guides and other stuff, it renders strangely. Is it on purpose or because of some rules on closures? I know we can do it differently, like:

fun(arg1, arg2, arg3) {}
// And then use a closure at the appropriate place
onEvent: (event) => fun('fromScope', 'alsoFromScope', event);

// Instead of:

fun(arg1, arg2) => (arg3) {};
// And then
onEvent: fun('fromScope', 'alsoFromScope');

It’s a similar result in the end, but it’s a slightly different code.
What do you think about this?

@munificent
Copy link
Member

Is it on purpose or because of some rules on closures?

It's a consequence of the general rule that the body of a function declaration using => is indented relative to the declaration itself. Curried style is really uncommon in Dart, so the formatter doesn't have any special rules to handle it better.

@ghivert
Copy link
Author

ghivert commented Oct 13, 2020

Do you think we could add this special case to the formatter ?

@munificent
Copy link
Member

I think it's unlikely. Nested functions are a difficult corner of the formatter's rules and it's hard to change how they are handled without producing worse output in other cases. If this was a common style, it would be worth the effort, but I almost never see Dart code like this. I'll leave the issue open and if I get time I can investigate it, but it likely won't be a high priority.

@yelliver
Copy link

I think this issue is a special case of my general issue #1083

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

3 participants