Search code examples
f#discriminated-union

Is there any way for me to fully qualify a discriminated union path?


Is there any way for me to fully qualify a discriminated union path?

I currently have this:

type PostionOfScott =
    | ScottOnFirst
    | ScottOnSecond
    | ScottOnThird

type PostionOfBrian =
    | BrianOnFirst
    | BrianOnSecond
    | BrianOnThird

type PostionOfCherice =
    | ChericeOnFirst
    | ChericeOnSecond
    | ChericeOnThird

I would like to do this:

type PostionOfScott =
    | First
    | Second
    | Third

type PostionOfBrian =
    | First
    | Second
    | Third

type PostionOfCherice =
    | First
    | Second
    | Third

However, when I provide the following code:

(*Functions*)
let hit play batterUp =

    match batterUp with
    | ScottAtBat   -> match play with
                      | Single  -> Scott First
                      | Double  -> Scott Second
                      | Tripple -> Scott Third

I receive the following error:

Error This expression was expected to have type PostionOfScott but here has type PostionOfCherice

I understand that the last declared discriminated union type will be referenced if there's any ambiguity.

However, is there any way for me to fully qualify a discriminated union path? Hence, I want to reduce verbiage within my union cases.


Solution

  • I think that repeating the position type definition for each player is probably not the best design choice. It would make more sense to keep these things separate and then your problem goes away naturally.

    type Position =
        | First
        | Second
        | Third
    
    type Player =
        | Scott
        | Brian
        | Cherice
    
    type PlayerPosition = {Player : Player; Position : Position}
    

    Then you can pattern match:

    let hit play batterUp =
        match batterUp with
        |{Player = Scott; Position = First} -> ...