I suggest we ...

Allow us to bind names multiple times in a pattern match

Why not allow us to use the same name multiple times in a pattern? I find myself writing code like this an awful lot:

match a with
| Some (x, y, z) when x = y && y = z && x = z ->
printfn "Something interesting"
| _ -> printfn "Something boring"

As shorthand for exactly this, why can't I write:

match a with
| Some (x, x, x) -> printfn "Something interesting"
| _ -> printfn "Something boring"

3 votes
Vote
Sign in
Check!
(thinking…)
Reset
or sign in with
  • facebook
  • google
    Password icon
    Signed in as (Sign out)
    You have left! (?) (thinking…)
    Richard Gibson shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

    This was considered in F# 1.0-2.0 but was not included.

    Among other things, the various notions of equality in F# (and indeed any programming language) are sufficiently subtle that it is better if uses of equality checks are made explicit at some point in the code.

    So while the feedback is appreciated, this kind of extension is beyond the scope of what we would consider for the F# language at this point.

    Thanks
    Don Syme, F# Language Evolution

    3 comments

    Sign in
    Check!
    (thinking…)
    Reset
    or sign in with
    • facebook
    • google
      Password icon
      Signed in as (Sign out)
      Submitting...
      • Marcus Magnusson commented  ·   ·  Flag as inappropriate

        I agree, I think this would be nice. Shows intention very clearly. I believe this is supported in Erlang, and the little experience I have of Erlang hasn't shown me any drawbacks to this.

      • luketopia commented  ·   ·  Flag as inappropriate

        I think this is a reasonable proposal. It's not contradictory to bind multiple values to a single pattern variable if they are equal. It's reads nicer than the when guard.

      • Isaac Abraham commented  ·   ·  Flag as inappropriate

        Because you're binding those fields to specific values for use on the right hand side of the -> - it's not for equality checks.

      F# Language

      Feedback and Knowledge Base