Last active
June 8, 2023 08:40
-
-
Save bent-rasmussen/af1b66150aa351f6ed16d397e963545f to your computer and use it in GitHub Desktop.
OrdinalIgnoreCaseString - an F# string type that is case-insensitive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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