Using active patterns within discrimated union type declarations
Asked Answered
C

3

5

Is it possible to use active patterns within discrimated union type declarations?

To be more precise, consider the following toy example:

type T = 
    | A of int
    | B

let (|Negative|_|) t = 
    match t with
    | A n when n < 0 -> Some ()
    | _ -> None

let T_ToString = function
    | Negative () -> "negative!"
    | _ -> "foo!"

Now suppose I want to override ToString() in T. Inside T's type declaration I can't refer to T_ToString since T_ToString is not yet declared at that point. I can't move the active pattern and T_ToString before ToString() because T is not yet declared at that point. But this doesn't work either:

type T = 
    | A of int
    | B

    static member (|Negative|_|) t = 
        match t with
        | A n when n < 0 -> Some ()
        | _ -> None

    override this.ToString () = 
        match this with
        | Negative () -> "negative!"
        | _ -> "foo!"
Chapman answered 12/12, 2012 at 14:31 Comment(2)
I have edited your title. Please see, "Should questions include “tags” in their titles?", where the consensus is "no, they should not".Rafaello
This answer may be relevant: "Active patterns should not be used as members. The fact that these compile at all is a compiler bug"Thermostat
P
4

Your question suggests that testing for negative is an intrinsic operation of T, so it should be part of its definition. Defining a property is one way to do that:

type T = 
  | A of int
  | B

  member this.IsNegative = 
    match this with
    | A n -> n < 0
    | _ -> false

  override this.ToString() = 
    if this.IsNegative then "negative!"
    else "foo!"

I'm not sure the active pattern is still needed, but if it is it's trivial:

let (|Negative|_|) (x: T) = if x.IsNegative then Some() else None
Patriciapatrician answered 12/12, 2012 at 15:22 Comment(0)
P
3

It's not the nicest solution, but you can do this:

type T = 
    | A of int
    | B

    static member internal ActivePattern t =
        match t with
        | A n when n < 0 -> Some ()
        | _ -> None

    override this.ToString () = 

        let (|Negative|_|) = T.ActivePattern

        match this with
        | Negative () -> "negative!"
        | _ -> "foo!"

let (|Negative|_|) = T.ActivePattern
Prolix answered 12/12, 2012 at 14:49 Comment(0)
C
2

OK, I think I found a solution: first declare the type, then declare the active pattern outside of it, and finally augment the type with an override implementation of ToString().

type T = 
    | A of int
    | B

let (|Negative|_|) t = 
    match t with
    | A n when n < 0 -> Some ()
    | _ -> None

type T with
    override this.ToString() = 
        match this with
        | Negative () -> "negative!"
        | _ -> "foo!"

However, this is not very nice as I get the warning

warning FS0060: Override implementations in augmentations are now deprecated. Override implementations should be given as part of the initial declaration of a type.
Chapman answered 12/12, 2012 at 14:43 Comment(1)
I think this suggests a design problem: an intrinsic operation, ToString, depends on an auxiliary function, (|Negative|_|). There's good reason this is awkward to do.Patriciapatrician

© 2022 - 2024 — McMap. All rights reserved.