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

[style-guide] Multiline application in patterns #717

Open
nojaf opened this issue Nov 3, 2022 · 2 comments
Open

[style-guide] Multiline application in patterns #717

nojaf opened this issue Nov 3, 2022 · 2 comments

Comments

@nojaf
Copy link
Contributor

nojaf commented Nov 3, 2022

Hello,

What should the guidance be for multiline (function?) applications in patterns?
Example:

match myString with
| Regex @"^(\w+) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds\.$" [ name; speed; flyTime; restTime ] ->
    ()

Imagine this pattern doesn't respect the max_line_length, how should it be formatted multiline?

This

match myString with
| Regex 
    @"^(\w+) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds\.$" 
    [ name; speed; flyTime; restTime ] ->
    ()

comes to mind.

Related, patterns do seem to have some funky offset rules from time to time.
The same example wrapped in a record:

match myString with
| { X = Regex 
            @"^(\w+) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds\.$" 
            [ name
              speed
              flyTime
              restTime ] } ->
    ()

Notice that:

match myString with
| { X = 
        Regex 
            @"^(\w+) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds\.$" 
            [ name
              speed
              flyTime
              restTime ] } ->
    ()

is invalid F# code. (online tool)

Another interesting case is:

match myString with
| X(
    Regex 
        @"^(\w+) can fly (\d+) km/s for (\d+) seconds, but then must rest for (\d+) seconds\.$" 
        [ name
          speed
          flyTime
          restTime ] 
  ) ->
    ()

This is valid code, thought the closing ) needs to be aligned with the X column.

@dsyme any thoughts on this?

@abelbraaksma
Copy link
Member

Glad you brought this up, though function application in patterns, where there's more than one argument, is pretty rare. More often this is coded to accept a tuple. But that's besides the point you're trying to make.

Isn't your first example invalid code? You have this in the list code: [ name speed flyTime restTime ], but in the follow-up, you have the equivalent of [ name; speed; flyTime; restTime ]. Which really are two different things.

Those offset rules are a bit worrying though.

TBH: my vote would probably be for leaving it on one line. That's probably gonna be controversial (isn't everything related to formatting?).

But then again, that pairs pretty badly with a multiline list, seq or array construction.

Hmm, I recant that idea. To be in line with the other function application rules, aligning them like your first example appears most sensible, tbh. But any which way I look at it, it's hard to think of any formatting that would improve the readability of such code...

@dsyme
Copy link
Contributor

dsyme commented Dec 7, 2022

I would say we should split like in the first example.

Related, patterns do seem to have some funky offset rules from time to time.

This seems like a bug and I can see it makes the splitting hard to implement.

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

No branches or pull requests

3 participants