I suggest we ...

Provide property on base Discriminated Union type if all the case constructors have the same paramater

If an each case constructor of Discriminated Union has a parameter with the same name and the same type than allow to implement a property on Discriminated Union base type to access to this parameter value.

Now I have to write a match on every case

type Physical =
| OneToOne of Data : ModuleData * Line : ModuleList
| OneOnOne of Data : ModuleData * Line1 : ModuleList * Line2 : ModuleList
| OneByOne of Data : ModuleData * Line1 : ModuleList * Line2 : ModuleList
| Vertical of Data : ModuleData * Line1 : ModuleList * Line2 : ModuleList
* Line3 : ModuleList * Line4 : ModuleList
member this.Data : ModuleData = match this with
| OneToOne(data, _) -> data
| OneOnOne(data, _, _) -> data
| OneByOne(data, _, _) -> data
| Vertical(data, _, _, _, _) -> data

Implement this same parameter as a field on base class and allow me to write

member this.Data : ModuleData = base.Data

Or automatic property implementation would also be a perfect option

27 votes
Vote
Sign in
(thinking…)
Password icon
Signed in as (Sign out)
You have left! (?) (thinking…)
Андрей Чебукин shared this idea  ·   ·  Flag idea as inappropriate…  ·  Admin →

2 comments

Sign in
(thinking…)
Password icon
Signed in as (Sign out)
Submitting...
  • Abel commented  ·   ·  Flag as inappropriate

    You can already do generalization:

    type Test =
        | Foo of string
        | Bar of string

    static member getString x =
        match x with
        | Foo s
        | Bar s -> s // both conditions wrapped in a single continuation

    It would, however, be nice if this is generalized further into something like:

    static member getString x =
        match x with
        | _ s -> s

    But that doesn't work well with tuples as in your original example. You'd still have to iterate them over, even though you'd need only one continuation.

  • exercitus vir commented  ·   ·  Flag as inappropriate

    This is too much magic for my taste. If I saw `base.Data` I would look for such a field or member, but would need to remember that F# magically transforms cases of tuples with common components to this.

F# Language

Feedback and Knowledge Base