How to access "base" in Discriminated Union method override? - f#

I have a DU and I'm overriding the Equals method. Based on the current DU value, I would like to call the base equality method or my custom one. However, it's not letting me access "base". Any idea on how to work around this?
type Test =
| A of string
| B of int64
override this.Equals(other) =
let other' = other :?> Test
match other' with
| A str -> str = "a"
| B i -> base.Equals this other //how do I do this?

First, any F# discriminated union will have obj as base class, so just use obj.Equals.
Second, Equals is a .NET method, not an F# function, so its arguments must be given in a tupled form - i.e. Equals(x,y) instead of Equals x y.
Finally, if you implement a custom Equals, you also need to add [<CustomEquality; NoComparison>]
So:
[<CustomEquality; NoComparison>]
type Test =
| A of string
| B of int64
override this.Equals(other) =
let other' = other :?> Test
match other' with
| A str -> str = "a"
| B i -> obj.Equals(this, other)

Related

Pattern matching against a string property

I'm de-serializing some mappings from JSON and later on I need to pattern match based on a string field of the de-serialized types like this:
let mappings = getWorkItemMappings
let result =
workItemMappings
|> Seq.find (fun (m: WorkItemMapping) -> m.Uuid = workTime.workItemUuid)
match mapping.Name with
Even if I complete the pattern match for all cases I still get Incomplete pattern matches on this expression.. Which is obvious to me due to the string type of the Name field.
Is there a way tell the compiler which values for the Name field are available?.
I think I could create a union type for the possible mapping types and try to de-serialize the JSON to this union type but I would like to if there's another option.
If you are pattern matching on a string value, the compiler has no static guarantee that it will only have certain values, because it is always possible to construct a string of a different value. The fact that it comes from JSON does not help - you may always have an invalid JSON.
The best option is to add a default case which throws a custom descriptive exception. Either one that you handle somewhere else (to indicate that the JSON file was invalid) or (if you check the validity elsewhere) something like this:
let parseFood f =
match f with
| "burger" -> 1
| "pizza" -> 2
| _ -> raise(invalidArg "f" $"Expected burger or pizza but got {f}")
Note that the F# compiler is very cautious. It does not even let you handle enum values using pattern matching, because under the cover, there are ways of creating invalid enum values! For example:
type Foo =
| A = 1
let f (a:Foo) =
match a with
| Foo.A -> 0
warning FS0104: Enums may take values outside known cases. For example, the value 'enum (0)' may indicate a case not covered by the pattern(s).
Very hard to understand what you're asking. Maybe this snippet can be of help. It demos how literal string constants can be used in pattern matching, and reused in functions. This gives some added safety and readability when adding and removing cases. If you prefer not to serialize a DU directly, then perhaps this is useful as part of the solution.
type MyDu =
| A
| B
| C
let [<Literal>] A' = "A"
let [<Literal>] B' = "B"
let [<Literal>] C' = "C"
let strToMyDuOption (s: string) =
match s with
| A' -> Some A
| B' -> Some B
| C'-> Some C
| _ -> None
let strToMyDu (s: string) =
match s with
| A' -> A
| B' -> B
| C'-> C
| s -> failwith $"MyDu case {s} is unknown."
let myDuToStr (x: MyDu) =
match x with
| A -> A'
| B -> B'
| C -> C'
// LINQPad
let dump x = x.Dump()
strToMyDuOption A' |> dump
strToMyDuOption "x" |> dump
myDuToStr A |> dump

Discriminated Union label to string

given the following Discriminated Union:
type A = B of string | C of int
How can I get the constructor B name?
A.B.ToString()
// would return something like:
val it : string = "FSI_0045+it#109-19"
// when I desire
val it : string = "B"
for example with this type it works:
type D = E | F
D.E.ToString();;
val it : string = "E"
I normally get the string name of an instance of the DU with
let stringFromDU (x: 'a) =
match FSharpValue.GetUnionFields(x, typeof<'a>) with
| case, _ -> case.Name
But in this case, I do not have an instance, I just want to serialize the label name.
If you enable the latest language version, e.g. by passing --langversion:preview to FSI or setting
<PropertyGroup>
<LangVersion>preview</LangVersion>
</PropertyGroup>
in your .fsproj, the following will work:
type A = B of int
let n = nameof A.B
Note: with F# 5 this will be supported out of the box :-)
You're using FSharpValue from FSharp.Reflection namespace in your example. Note that there's another class in that library for handling scenarios where you want to work with types only, FSharpType.
let cases = FSharpType.GetUnionCases(typeof<A>)
Outside of unions, it also provides helpers for other operations on F# native types.

Why are there two different syntaxes for CustomEquality and CustomComparison in F#?

I want to implement a type Symbol in F#, which has an associated string and position (let's say a line number in text). I would do as follows:
type Symbol = Symbol of string * int // (string, line number)
I want a custom equality that dismisses the line number. I will have a "strict equality" that takes the line number into account, but I want the default equality to compare only the strings. Looking at this SO post, it seems one has to do as follows:
[<CustomEquality; CustomComparison>]
type Symbol =
| Symbol of string * int
member x.GetString() =
match x with
| Symbol (s, _) -> s
override x.Equals(y) = // Equality only compares strings, not positions.
match y with
| :? Symbol as i -> i.GetString() = x.GetString()
| _ -> false
override x.GetHashCode() =
match x with
| Symbol (s, p) -> hash (s, p)
However, to implement custom comparison, one has to add below the above declaration
interface System.IComparable with
member x.CompareTo(yobj) =
match yobj with
| :? Symbol as y -> compare (x.GetString()) (y.GetString())
| _ -> invalidArg "yobj" "cannot compare values of different types"
Why can I write override x.Equals(y)..., but not override x.CompareTo(yobj)...? Why do I have to specify interface System.IComparable with ...? It seems there exists a System.IEquatable, but I do not need to specify it, why? It is not a big difference, but I was just wondering why the difference was here.
The difference is that for Equals you're overriding Object.Equals - which is a virtual method on a base class, while for CompareTo you're implementing an interface (which in F# requires explicit implementation via "interface .. with".

Dynamic cast to Interface

According to the post http://cs.hubfs.net/forums/thread/3616.aspx,
I need to use a function like the following to cast an object to an interface, I have run a test, this is still true, the bug of :?> is still not fixed.
let cast<'a> o = (box o) :?> 'a
let ci = { new Customer(18, Name = "fred") with
override x.ToString() = x.Name
interface ITalk with
member x.Talk() =
printfn "talk1111111" }
let italk = cast<ITalk> ci
if not (italk = null) then
italk.Talk()
Is there a more elegant way to write the above code. I am thinking to create another operator to replace :?>, but I can not get the generic type parameter passed in like the :?>
Your cast function does not behave like the C# as operator - if the object can't be cast to the specified type, it will throw an exception rather than returning null. Therefore, checking to see if italk = null accomplishes nothing. If you want to make the cast function return null when the cast fails instead of throwing an exception, you could write it like this:
let cast<'a when 'a : null> o =
match box o with
| :? 'a as output -> output
| _ -> null
However, this will only work on nullable types, which does not include structs or (by default) F# types. I might leave your cast function the way it is, and make a tryCast that uses options.
let tryCast<'a> o =
match box o with
| :? 'a as output -> Some output
| _ -> None
Then you could use it like this:
ci |> tryCast<ITalk> |> Option.iter (fun it -> it.Talk())
In this case, Option.iter takes the place of your null test.
Pattern matching provides a more idiomatic way to write this:
match box ci with
| :? ITalk as italk -> italk.Talk()
| _ -> ()
Or, even:
let bci = box ci
if bci :? ITalk then (bci :?> ITalk).Talk()
I keep a function like the following around, for when I know the type test will hold:
let coerce value = (box >> unbox) value
(coerce ci : ITalk).Talk()

Randomly choose an instance from union in F#

In F#, given
type MyType = A | B | C | D | E | F | G
How do I randomly define an instance of MyType?
This ought to work:
let randInst<'t>() =
let cases = Reflection.FSharpType.GetUnionCases(typeof<'t>)
let index = System.Random().Next(cases.Length)
let case = cases.[index]
Reflection.FSharpValue.MakeUnion(case, [||]) :?> 't
This code assumes that the union cases are all nullary and that the type you're using is actually a union type, but it would be easy to explicitly check those assumptions and throw meaningful exceptions if desired.
Select a random number, then pattern match that number with different branches returning a different instant?

Resources