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

Patterns for self identifiers #752

Open
Happypig375 opened this issue Jun 14, 2019 · 8 comments

Comments

Projects
None yet
6 participants
@Happypig375
Copy link
Contributor

commented Jun 14, 2019

Patterns for self identifiers

I propose we allow patterns for self identifiers.

The existing way of approaching this problem in F# is an explicit let binding.

type NumberWrapper = NumberWrapper of int with
    member this.Map f =
        let (NumberWrapper n) = this
        f n

With pattern matching for self identifiers, we can write

type NumberWrapper = NumberWrapper of int with
    member (NumberWrapper n).Map f =
        f n

Pros and Cons

The advantages of making this adjustment to F# are

  1. Encouraging the use of patterns - a fundamental F# construct.
  2. Adding emphasis that self identifiers are actually ordinary parameters - unlike C#.
  3. Enabling more concise code - the purpose of pattern matching.

The disadvantage of making this adjustment to F# is allowing for too much conciseness?

type NumberWrapper = NumberWrapper of int with
    member (NumberWrapper (SomeActivePattern (item, KeyValue (Lazy key, value)))).Map f =
        f (item, key, value)

But isn't this already allowed for parameters? This is just matching them.

Extra information

Estimated cost (XS, S, M, L, XL, XXL): S

Related suggestions: (put links to related suggestions here)

Affidavit (please submit!)

Please tick this by placing a cross in the box:

  • This is not a question (e.g. like one you might ask on stackoverflow) and I have searched stackoverflow for discussions of this issue
  • I have searched both open and closed suggestions on this site and believe this is not a duplicate
  • This is not something which has obviously "already been decided" in previous versions of F#. If you're questioning a fundamental design decision that has obviously already been taken (e.g. "Make F# untyped") then please don't submit it.

Please tick all that apply:

  • This is not a breaking change to the F# language design
  • I or my company would be willing to help implement and/or test this
@pblasucci

This comment has been minimized.

Copy link

commented Jun 14, 2019

💯 This is a feature I've always wanted... though I do wonder how it would interact with exhaustiveness checking? Perhaps it could only be enabled/supported for single-case unions (though that seems somehow inconsistent with the general "feel" of the language)?

@Happypig375

This comment has been minimized.

Copy link
Contributor Author

commented Jun 14, 2019

Just like with let bindings. Warnings are given for unexhausted matches.

@pblasucci

This comment has been minimized.

Copy link

commented Jun 14, 2019

Aside...

It's seems like there are enough situations where one wants to treat "single-case unions" specially. Too bad newtype-like functionality was abandoned years ago.

@pblasucci

This comment has been minimized.

Copy link

commented Jun 14, 2019

@Happypig375

Just like with let bindings. Warnings are given for unexhausted matches.

☝️ I could live with that (I think?)

@charlesroddie

This comment has been minimized.

Copy link

commented Jun 24, 2019

The let pattern = z syntax is useful for:

  • tuples (let (x,_)=), but tuples cannot define instance members
  • records (let {X:x; Y:_} = ), which would work in this case but look strange (member {X:x; Y:y}.sum = x + y)
  • single case DUs which are bad taste as gratuitous complexity

So it comes down to: should records allow member {X:x; Y:y}.sum = x + y syntax? Seems reasonable but not sure that this is worth it.

This feature overlaps with #333 .

@cartermp

This comment has been minimized.

Copy link
Member

commented Jun 26, 2019

This has some limited utility I think. The primary use case isn't too bad:

type NumberWrapper = NumberWrapper of int with
    member __.Map f = function
        NumberWrapper n -> f n

And for multi-case DUs I think most people would prefer a pattern match on this anyways. I'm not necessarily opposed, but I'm not sure how valuable this is.

@gusty

This comment has been minimized.

Copy link

commented Jul 5, 2019

I see this more as a technical limitation than a feature request.

I mean, while learning F# you might think this is already supported and probably all of us realized it is not by trying it.

The spec doesn't specify clearly this doesn't work and by reading about patterns and bindings the user tends to think this should work already. So one could see this limitation as a bug.

@realvictorprm

This comment has been minimized.

Copy link
Member

commented Jul 5, 2019

I think the same like @gusty

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.