Skip to content

Instantly share code, notes, and snippets.

@bent-rasmussen
Last active June 8, 2023 08:40
Show Gist options
  • Save bent-rasmussen/af1b66150aa351f6ed16d397e963545f to your computer and use it in GitHub Desktop.
Save bent-rasmussen/af1b66150aa351f6ed16d397e963545f to your computer and use it in GitHub Desktop.
OrdinalIgnoreCaseString - an F# string type that is case-insensitive
// Experiment
open System
open System.Collections.Generic
open System.Runtime.CompilerServices
// Case-insensitive strings for ValueTuple embedding.
[<IsReadOnly; Struct; CustomEquality; CustomComparison>]
type OrdinalIgnoreCaseString =
{ Value: string }
override this.GetHashCode() = StringComparer.OrdinalIgnoreCase.GetHashCode(this.Value)
interface IEquatable<OrdinalIgnoreCaseString> with
member this.Equals other = StringComparer.OrdinalIgnoreCase.Equals(this.Value, other.Value)
override this.Equals other =
match other with
| :? OrdinalIgnoreCaseString as x -> (this :> IEquatable<_>).Equals x
| _ -> false
interface IComparable<OrdinalIgnoreCaseString> with
member this.CompareTo other = StringComparer.OrdinalIgnoreCase.Compare(this.Value, other.Value)
interface IComparable with
member this.CompareTo other =
match other with
| :? OrdinalIgnoreCaseString as x -> (this :> IComparable<_>).CompareTo x
| _ -> -1
override this.ToString() = this.Value
let x = { Value = "foo" }
let y = { Value = "FoO" }
let z = { Value = "fo0" }
printfn $"'{x}' = '{y}' => {x = y}"
printfn $"'{x}' = '{z}' => {x = z}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment