Posts

Showing posts with the label f#

Using a Map with a non-comparable object

Using a Map with a non-comparable object I have Job type, equality is defined as the Job's ID being equal. There should never be two jobs with the same ID. They aren't comparable though, one job isn't more or less than another, only equal or not. Job type JobId = JobId of string [<CustomEquality; NoComparison>] type Job = { Id: JobId } with interface System.IEquatable<Job> with member x.Equals y = x.Id = y.Id type Resource = { Id: string Capacity: float Usage: Map<Job,float> } The Map needs a comparison though. Map Map IDictionary I would advise you to use JobId as the map key, not Job , and don't override Equals , just check for ID equality. It's a more functional/idiomatic F# style: data is just dumb data. And it makes this particular problem go away. – TheQuickBrownFox Jul 2 at 9:37 ...

Why is compiler ok with not closed generic?

Why is compiler ok with not closed generic? I have this code: open System let func<'t when 't:comparison> (a: 't) = a [<EntryPoint>] let main argv = let array = [||] let actual = func array printfn "array = %A, actual = %A, same objects: %b" array actual (Object.ReferenceEquals(array, actual)) Console.ReadKey() 0 When I try it in LinqPad5 I get a reasonlable error: Value restriction. The value 'actual' has been inferred to have generic type val actual : '_a when '_a : comparison Either define 'actual' as a simple data term, make it a function with explicit arguments or, if you do not intend for it to be generic, add a type annotation. However, when I successfully (!) compile and run it (checked for full .NET Framework and DotNetCore both Debug/Release) in Visual Studio I get this output: array = [||], actual = [||], same objects: false The only way I could expect this result if 't were a...