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

Suggestion: Allow active patterns in arguments of primary constructors #12720

Closed
En3Tho opened this issue Feb 11, 2022 · 7 comments
Closed

Suggestion: Allow active patterns in arguments of primary constructors #12720

En3Tho opened this issue Feb 11, 2022 · 7 comments

Comments

@En3Tho
Copy link
Contributor

En3Tho commented Feb 11, 2022

For some reason they don't work... Or some kind of workaround is needed ?

module Test =
    let inline (|NotNull|) (obj: 'a when 'a: not struct) = ArgumentNullException.ThrowIfNull(obj); obj
    let curriedFunction (NotNull z) y = z // works
    let tupledFunction(NotNull z, y) = z // works

    type PrimaryCtor(NotNull a: string) = class end // doesn't work?
    
    type SecondaryCtor =
        private new() = SecondaryCtor()
        new (NotNull a: string) = SecondaryCtor() // works

This is partially motivated by C# discussion of !! feature. In F# we can easily make it much readable and explicit by using ActivePatterns but for some reasons do not work with PrimaryConstructors or a workaround is needed.

So those a kinda related fsharp/fslang-suggestions#966 and fsharp/fslang-suggestions#1014 (look at active patterns example below)

@baronfel
Copy link
Member

Hi @En3Tho! Changes to the language need to go through fslang-suggestions instead of being raised here. Would you mind opening this suggestion there?

@En3Tho
Copy link
Contributor Author

En3Tho commented Feb 11, 2022

@baronfel Yeah. I just wasn't really sure if this is a bug or a feature request actually. I believe it does have a smell of a bug but maybe I'm missing something important.

What do you think?

@baronfel
Copy link
Member

I went to go look in the parser and explicitly only a subset of simple patterns are allowed, which suggests to me that it was a design decision rather than an oversight. To me that means a language change is needed.

@brianrourkeboll
Copy link
Contributor

brianrourkeboll commented Feb 11, 2022

Yeah, the spec (§8.6.1) says nested patterns can't be used in primary constructors:

The pattern for a primary constructor must have zero or more patterns of the following form:

(simple-pat, ..., simple-pat)

Each simple-pat has this form:

simple-pat :=
| ident
| simple-pat : type

Specifically, nested patterns may not be used in the primary constructor arguments. For example,
the following is not permitted because the primary constructor arguments contain a nested tuple
pattern:

type TwoVectors((px, py), (qx, qy)) =
member v.Length = sqrt((qx-px)*(qx-px) + (qy-py)*(qy-py))

Instead, one or more value definitions should be used to accomplish the same effect:

type TwoVectors(pv, qv) =
let (px, py) = pv
let (qx, qy) = qv
member v.Length = sqrt((qx-px)*(qx-px) + (qy-py)*(qy-py))

@En3Tho
Copy link
Contributor Author

En3Tho commented Feb 11, 2022

Curious. Wonder why that decision was made. @dsyme can you please tell us about it?

I don't know when exactly that decision was made but does it have an important meaning now? This behavior seems sort of outstanding.

@dsyme
Copy link
Contributor

dsyme commented Feb 16, 2022

Curious. Wonder why that decision was made. @dsyme can you please tell us about it?

IIRC the reason is that the pattern can fail to match, and according to the verifiability rules of .NET IL we couldn't codegen the exception-raising before the inherits call is called.

Things may have changed and it's now possible to do this (partly because verifiability is less of a concern), I'm not sure.

@dsyme
Copy link
Contributor

dsyme commented Feb 16, 2022

In any case yes please add to fslang-suggestions

@dsyme dsyme closed this as completed Feb 16, 2022
@dsyme dsyme changed the title Enabled active patterns usage in primary constructors Suggestion: Allow active patterns in arguments of primary constructors Feb 16, 2022
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

4 participants