System.NullReferenceException is thrown from presumably valid function. When the argument of the function is annotated with the same type that was the inferred type the problem does not reproduce.
Repro steps
The issue can be reproduced with the code below.
let weightedMedian (weightedList : (float<'u> * float<'v>) list) =
let sumOfWeights = List.sumBy fst weightedList
if sumOfWeights <= 0.0<_> then
invalidArg "weightedList" "Sum of weights must be positive"
let rec loop accumululatedWeight (remaining : (float<'u> * float<'v>) list) =
match remaining with
| [] -> failwith "Input list to weightedMedian must not be empty!"
| (weight, value) :: _ when accumululatedWeight + weight >= 0.5 * sumOfWeights ->
value
| (weight, _) :: tail ->
loop (accumululatedWeight + weight) tail
loop 0.0<_> (List.sortBy snd weightedList)
let weightedMedian2 weightedList =
let sumOfWeights = List.sumBy fst weightedList
if sumOfWeights <= 0.0<_> then
invalidArg "weightedList" "Sum of weights must be positive"
let rec loop accumululatedWeight (remaining : (float<'u> * float<'v>) list) =
match remaining with
| [] -> failwith "Input list to weightedMedian must not be empty!"
| (weight, value) :: _ when accumululatedWeight + weight >= 0.5 * sumOfWeights ->
value
| (weight, _) :: tail ->
loop (accumululatedWeight + weight) tail
loop 0.0<_> (List.sortBy snd weightedList)
weightedMedian [ (0.3, 1.0); (0.3, 2.0); (0.4, 3.0) ] // Returns 2.0 as expected
weightedMedian2 [ (0.3, 1.0); (0.3, 2.0); (0.4, 3.0) ] // Throws System.NullReferenceException: Object reference not set to an instance of an object.
Expected behavior
weightedMedian and weightedMedian2 should behave identically.
Actual behavior
weightedMedian works as expected while weightedMedian2 always throws System.NullReferenceException.
Related information
- Operating system: Windows 7
- .NET Framework 4.5
- F# Interactive version 14.0.23413.0
System.NullReferenceExceptionis thrown from presumably valid function. When the argument of the function is annotated with the same type that was the inferred type the problem does not reproduce.Repro steps
The issue can be reproduced with the code below.
Expected behavior
weightedMedianandweightedMedian2should behave identically.Actual behavior
weightedMedianworks as expected whileweightedMedian2always throwsSystem.NullReferenceException.Related information