Skip to content

Commit ea0afa6

Browse files
gustywallymathieu
authored andcommitted
Use nullArgCheck and pin signatures (#653)
1 parent 5fafab0 commit ea0afa6

File tree

13 files changed

+791
-284
lines changed

13 files changed

+791
-284
lines changed

docsrc/tools/doclib.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ module DocLib
44
// Copyright 2010 Steffen Forkmann
55
// Apache license
66

7-
7+
#nowarn "49"
88
open System.IO
99

1010
module String =

src/FSharpPlus/Extensions/Array.fs

Lines changed: 121 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ module Array =
3333
/// <param name="value">The element to add</param>
3434
/// <param name="array">The array to add to</param>
3535
/// <returns>A new array with the element added to the beginning.</returns>
36-
let cons value array =
36+
let cons value (array: 'T []) =
3737
#if !NET45
38-
raiseIfNull (nameof(array)) array
38+
let array = nullArgCheck (nameof array) array
3939
Array.insertAt 0 value array
4040
#else
4141
raiseIfNull "array" array
@@ -46,10 +46,10 @@ module Array =
4646
/// <param name="array">The input array.</param>
4747
/// <returns>A tuple with the head and the tail of the original array.</returns>
4848
/// <exception cref="T:System.ArgumentException">Thrown when the input array is empty.</exception>
49-
let uncons array =
49+
let uncons (array: 'T []) =
5050
#if !NET45
51-
raiseIfNull (nameof(array)) array
52-
if Array.isEmpty array then invalidArg (nameof(array)) LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
51+
let array = nullArgCheck (nameof array) array
52+
if Array.isEmpty array then invalidArg (nameof array) LanguagePrimitives.ErrorStrings.InputSequenceEmptyString
5353
else array[0], array[1..]
5454
#else
5555
raiseIfNull "array" array
@@ -68,50 +68,69 @@ module Array =
6868
/// val it : int [] = [|2; 4; 6; 3; 6; 9|]
6969
/// </code>
7070
/// </example>
71-
let apply f x =
71+
let apply (f: ('T -> 'U) []) (x: 'T []) : 'U [] =
7272
#if !NET45
73-
raiseIfNull (nameof(x)) x
73+
let x = nullArgCheck (nameof x) x
74+
#else
75+
raiseIfNull "x" x
7476
#endif
7577

7678
let lenf, lenx = Array.length f, Array.length x
7779
Array.init (lenf * lenx) (fun i -> let (d, r) = Math.DivRem (i, lenx) in f.[d] x.[r])
7880

79-
/// Combines all values from the first array with the second, using the supplied mapping function.
80-
let lift2 f x y =
81+
/// <summary>Combines all values from three arrays and calls a mapping function on this combination.</summary>
82+
/// <param name="mapping">Mapping function taking three element combination as input.</param>
83+
/// <param name="array1">First array.</param>
84+
/// <param name="array2">Second array.</param>
85+
///
86+
/// <returns>Array with values returned from mapping function.</returns>
87+
let lift2 (mapping: 'T1 -> 'T2 -> 'U) (array1: 'T1 []) (array2: 'T2 []) : 'U [] =
8188
#if !NET45
82-
raiseIfNull (nameof(x)) x
83-
raiseIfNull (nameof(y)) y
89+
let array1 = nullArgCheck (nameof array1) array1
90+
let array2 = nullArgCheck (nameof array2) array2
91+
#else
92+
raiseIfNull "array1" array1
93+
raiseIfNull "array2" array2
8494
#endif
8595

86-
let lenx, leny = Array.length x, Array.length y
87-
Array.init (lenx * leny) (fun i -> let (d, r) = Math.DivRem (i, leny) in f x.[d] y.[r])
96+
let lenx, leny = Array.length array1, Array.length array2
97+
Array.init (lenx * leny) (fun i -> let (d, r) = Math.DivRem (i, leny) in mapping array1.[d] array2.[r])
8898

8999

90100
/// <summary>Combines all values from three arrays and calls a mapping function on this combination.</summary>
91101
/// <param name="mapping">Mapping function taking three element combination as input.</param>
92-
/// <param name="list1">First array.</param>
93-
/// <param name="list2">Second array.</param>
94-
/// <param name="list3">Third array.</param>
102+
/// <param name="array1">First array.</param>
103+
/// <param name="array2">Second array.</param>
104+
/// <param name="array3">Third array.</param>
95105
///
96106
/// <returns>Array with values returned from mapping function.</returns>
97-
let lift3 mapping list1 list2 list3 =
107+
let lift3 (mapping: 'T1 -> 'T2 -> 'T3 -> 'U) (array1: 'T1 []) (array2: 'T2 []) (array3: 'T3 []) : 'U [] =
98108
#if !NET45
99-
raiseIfNull (nameof(list1)) list1
100-
raiseIfNull (nameof(list2)) list2
101-
raiseIfNull (nameof(list3)) list3
109+
let array1 = nullArgCheck (nameof array1) array1
110+
let array2 = nullArgCheck (nameof array2) array2
111+
let array3 = nullArgCheck (nameof array3) array3
112+
#else
113+
raiseIfNull "array1" array1
114+
raiseIfNull "array2" array2
115+
raiseIfNull "array3" array3
102116
#endif
103117

104-
let lenx, leny, lenz = Array.length list1, Array.length list2, Array.length list3
105-
let combinedFirstTwo = Array.init (lenx * leny) (fun i -> let (d, r) = Math.DivRem (i, leny) in (list1.[d], list2.[r]))
118+
let lenx, leny, lenz = Array.length array1, Array.length array2, Array.length array3
119+
let combinedFirstTwo = Array.init (lenx * leny) (fun i -> let (d, r) = Math.DivRem (i, leny) in (array1.[d], array2.[r]))
106120

107-
Array.init (lenx * leny * lenz) (fun i -> let (d, r) = Math.DivRem (i, lenz) in combinedFirstTwo.[d], list3.[r])
121+
Array.init (lenx * leny * lenz) (fun i -> let (d, r) = Math.DivRem (i, lenz) in combinedFirstTwo.[d], array3.[r])
108122
|> Array.map (fun x -> mapping (fst (fst x)) (snd (fst x)) (snd x))
109123

110124
/// Concatenates all elements, using the specified separator between each element.
111-
let intercalate (separator: 'T []) (source: seq<'T []>) =
125+
let intercalate (separator: 'T []) (source: seq<'T []>) : 'T [] =
112126
#if !NET45
113-
raiseIfNull (nameof(source)) source
127+
let separator = nullArgCheck (nameof separator) separator
128+
let source = nullArgCheck (nameof source) source
129+
#else
130+
raiseIfNull "separator" separator
131+
raiseIfNull "source" source
114132
#endif
133+
115134
#if FABLE_COMPILER || NET45
116135
source |> Seq.intercalate separator |> Seq.toArray
117136
#else
@@ -125,9 +144,11 @@ module Array =
125144
#endif
126145

127146
/// Inserts a separator element between each element in the source array.
128-
let intersperse element (source: 'T []) =
147+
let intersperse element (source: 'T []) : 'T [] =
129148
#if !NET45
130-
raiseIfNull (nameof(source)) source
149+
let source = nullArgCheck (nameof source) source
150+
#else
151+
raiseIfNull "source" source
131152
#endif
132153

133154
match source with
@@ -140,20 +161,27 @@ module Array =
140161
| _ -> element)
141162

142163
/// Creates a sequence of arrays by splitting the source array on any of the given separators.
143-
let split (separators: seq<_ []>) (source: _ []) =
164+
let split (separators: seq<'T []>) (source: 'T []) : seq<'T []> =
144165
#if !NET45
145-
raiseIfNull (nameof(separators)) separators
146-
raiseIfNull (nameof(source)) source
166+
let separators = nullArgCheck (nameof separators) separators
167+
let source = nullArgCheck (nameof source) source
168+
#else
169+
raiseIfNull "separators" separators
170+
raiseIfNull "source" source
147171
#endif
148172

149173
source |> Array.toSeq |> Seq.split separators |> Seq.map Seq.toArray
150174

151175
/// Replaces a subsequence of the source array with the given replacement array.
152176
let replace (oldValue: 'T seq) (newValue: 'T seq) (source: 'T[]) : 'T[] =
153177
#if !NET45
154-
raiseIfNull (nameof(oldValue)) oldValue
155-
raiseIfNull (nameof(newValue)) newValue
156-
raiseIfNull (nameof(source)) source
178+
let oldValue = nullArgCheck (nameof oldValue) oldValue
179+
let newValue = nullArgCheck (nameof newValue) newValue
180+
let source = nullArgCheck (nameof source) source
181+
#else
182+
raiseIfNull "oldValue" oldValue
183+
raiseIfNull "newValue" newValue
184+
raiseIfNull "source" source
157185
#endif
158186
#if FABLE_COMPILER || NET45
159187
source |> Array.toSeq |> Seq.replace oldValue newValue |> Seq.toArray: 'T []
@@ -222,10 +250,13 @@ module Array =
222250
/// <returns>
223251
/// The index of the slice or <c>None</c>.
224252
/// </returns>
225-
let findSliceIndex (slice: _ []) (source: _ []) =
253+
let findSliceIndex (slice: 'T []) (source: 'T []) : int =
226254
#if !NET45
227-
raiseIfNull (nameof(slice)) slice
228-
raiseIfNull (nameof(source)) source
255+
let slice = nullArgCheck (nameof slice) slice
256+
let source = nullArgCheck (nameof source) source
257+
#else
258+
raiseIfNull "slice" slice
259+
raiseIfNull "source" source
229260
#endif
230261

231262
let index = Internals.FindSliceIndex.arrayImpl slice source
@@ -241,10 +272,13 @@ module Array =
241272
/// <returns>
242273
/// The index of the slice or <c>None</c>.
243274
/// </returns>
244-
let tryFindSliceIndex (slice: _ []) (source: _ []) =
275+
let tryFindSliceIndex (slice: 'T []) (source: 'T []) : int option =
245276
#if !NET45
246-
raiseIfNull (nameof(slice)) slice
247-
raiseIfNull (nameof(source)) source
277+
let slice = nullArgCheck (nameof slice) slice
278+
let source = nullArgCheck (nameof source) source
279+
#else
280+
raiseIfNull "slice" slice
281+
raiseIfNull "source" source
248282
#endif
249283

250284
let index = Internals.FindSliceIndex.arrayImpl slice source
@@ -257,10 +291,13 @@ module Array =
257291
/// <returns>
258292
/// The index of the slice or <c>None</c>.
259293
/// </returns>
260-
let findLastSliceIndex (slice: _ []) (source: _ []) =
294+
let findLastSliceIndex (slice: _ []) (source: _ []) : int =
261295
#if !NET45
262-
raiseIfNull (nameof(slice)) slice
263-
raiseIfNull (nameof(source)) source
296+
let slice = nullArgCheck (nameof slice) slice
297+
let source = nullArgCheck (nameof source) source
298+
#else
299+
raiseIfNull "slice" slice
300+
raiseIfNull "source" source
264301
#endif
265302

266303
let index = Internals.FindLastSliceIndex.arrayImpl slice source
@@ -276,10 +313,13 @@ module Array =
276313
/// <returns>
277314
/// The index of the slice or <c>None</c>.
278315
/// </returns>
279-
let tryFindLastSliceIndex (slice: _ []) (source: _ []) =
316+
let tryFindLastSliceIndex (slice: 'T []) (source: 'T []) : int option =
280317
#if !NET45
281-
raiseIfNull (nameof(slice)) slice
282-
raiseIfNull (nameof(source)) source
318+
let slice = nullArgCheck (nameof slice) slice
319+
let source = nullArgCheck (nameof source) source
320+
#else
321+
raiseIfNull "slice" slice
322+
raiseIfNull "source" source
283323
#endif
284324

285325
let index = Internals.FindLastSliceIndex.arrayImpl slice source
@@ -293,9 +333,11 @@ module Array =
293333
/// <returns>
294334
/// A tuple with both resulting arrays.
295335
/// </returns>
296-
let partitionMap (mapper: 'T -> Choice<'T1,'T2>) (source: array<'T>) =
336+
let partitionMap (mapper: 'T -> Choice<'T1, 'T2>) (source: array<'T>) : array<'T1> * array<'T2> =
297337
#if !NET45
298-
raiseIfNull (nameof(source)) source
338+
let source = nullArgCheck (nameof source) source
339+
#else
340+
raiseIfNull "source" source
299341
#endif
300342

301343
let (x, y) = ResizeArray (), ResizeArray ()
@@ -305,22 +347,29 @@ module Array =
305347
/// <summary>Safely build a new array whose elements are the results of applying the given function
306348
/// to each of the elements of the two arrays pairwise.</summary>
307349
/// <remark>If one array is shorter, excess elements are discarded from the right end of the longer array.</remark>
308-
let map2Shortest f (a1: 'T []) (a2: 'U []) =
350+
let map2Shortest (f: 'T1 -> 'T2 -> 'U) (a1: 'T1 []) (a2: 'T2 []) : 'U [] =
309351
#if !NET45
310-
raiseIfNull (nameof(a1)) a1
311-
raiseIfNull (nameof(a2)) a2
352+
let a1 = nullArgCheck (nameof a1) a1
353+
let a2 = nullArgCheck (nameof a2) a2
354+
#else
355+
raiseIfNull "a1" a1
356+
raiseIfNull "a2" a2
312357
#endif
313358

314359
Array.init (min a1.Length a2.Length) (fun i -> f a1.[i] a2.[i])
315360

316361
/// <summary>Safely build a new array whose elements are the results of applying the given function
317362
/// to each of the elements of the three arrays pairwise.</summary>
318363
/// <remark>If one array is shorter, excess elements are discarded from the right end of the longer array.</remark>
319-
let map3Shortest f (a1: 'T1 []) (a2: 'T2 []) (a3: 'T3 []) =
364+
let map3Shortest (f: 'T1 -> 'T2 -> 'T3 -> 'U) (a1: 'T1 []) (a2: 'T2 []) (a3: 'T3 []) : 'U [] =
320365
#if !NET45
321-
raiseIfNull (nameof a1) a1
322-
raiseIfNull (nameof a2) a2
323-
raiseIfNull (nameof a3) a3
366+
let a1 = nullArgCheck (nameof a1) a1
367+
let a2 = nullArgCheck (nameof a2) a2
368+
let a3 = nullArgCheck (nameof a3) a3
369+
#else
370+
raiseIfNull "a1" a1
371+
raiseIfNull "a2" a2
372+
raiseIfNull "a3" a3
324373
#endif
325374
Array.init (min a1.Length a2.Length |> min a3.Length) (fun i -> f a1.[i] a2.[i] a3.[i])
326375

@@ -330,10 +379,13 @@ module Array =
330379
/// <param name="a1">First input array.</param>
331380
/// <param name="a2">Second input array.</param>
332381
/// <returns>Array with corresponding pairs of input arrays.</returns>
333-
let zipShortest (a1: array<'T1>) (a2: array<'T2>) =
382+
let zipShortest (a1: array<'T1>) (a2: array<'T2>) : array<'T1 * 'T2> =
334383
#if !NET45
335-
raiseIfNull (nameof(a1)) a1
336-
raiseIfNull (nameof(a2)) a2
384+
let a1 = nullArgCheck (nameof a1) a1
385+
let a2 = nullArgCheck (nameof a2) a2
386+
#else
387+
raiseIfNull "a1" a1
388+
raiseIfNull "a2" a2
337389
#endif
338390

339391
Array.init (min a1.Length a2.Length) (fun i -> a1.[i], a2.[i])
@@ -345,11 +397,15 @@ module Array =
345397
/// <param name="a2">Second input array.</param>
346398
/// <param name="a3">Third input array.</param>
347399
/// <returns>Array with corresponding tuple of input arrays.</returns>
348-
let zip3Shortest (a1: array<'T1>) (a2: array<'T2>) (a3: array<'T3>) =
400+
let zip3Shortest (a1: array<'T1>) (a2: array<'T2>) (a3: array<'T3>) : array<'T1 * 'T2 * 'T3> =
349401
#if !NET45
350-
raiseIfNull (nameof a1) a1
351-
raiseIfNull (nameof a2) a2
352-
raiseIfNull (nameof a3) a3
402+
let a1 = nullArgCheck (nameof a1) a1
403+
let a2 = nullArgCheck (nameof a2) a2
404+
let a3 = nullArgCheck (nameof a3) a3
405+
#else
406+
raiseIfNull "a1" a1
407+
raiseIfNull "a2" a2
408+
raiseIfNull "a3" a3
353409
#endif
354410
Array.init (min a1.Length a2.Length |> min a3.Length) (fun i -> a1.[i], a2.[i], a3.[i])
355411

@@ -358,9 +414,11 @@ module Array =
358414
/// <param name="source">The input array.</param>
359415
///
360416
/// <returns>Array with values x for each Array value where the function returns Some(x).</returns>
361-
let choosei mapping source =
417+
let choosei (mapping: int -> 'T -> 'U option) (source: array<'T>) : array<'U> =
362418
#if !NET45
363-
raiseIfNull (nameof(source)) source
419+
let source = nullArgCheck (nameof source) source
420+
#else
421+
raiseIfNull "source" source
364422
#endif
365423

366424
let mutable i = ref -1

src/FSharpPlus/Extensions/Enumerator.fs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace FSharpPlus
22

3+
#nowarn "3261" // nullness checks
34
#if (!FABLE_COMPILER || FABLE_COMPILER_3) && !FABLE_COMPILER_4
45

56
/// Additional operations on IEnumerator

src/FSharpPlus/Extensions/Exception.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ module Exception =
1212
/// Throws the given exception with its original stacktrace.
1313
let inline rethrow<'T> (exn: exn) =
1414
#if !NET45
15-
raiseIfNull (nameof exn) exn
15+
let exn = nullArgCheck (nameof exn) exn
1616
#endif
1717
(ExceptionDispatchInfo.Capture exn).Throw ()
1818
Unchecked.defaultof<'T>
@@ -21,8 +21,8 @@ module Exception =
2121
/// Exceptions already present in the first argument won't be added.
2222
let add (exn1: exn) (exn2: exn) =
2323
#if !NET45
24-
raiseIfNull (nameof exn1) exn1
25-
raiseIfNull (nameof exn2) exn2
24+
let exn1 = nullArgCheck (nameof exn1) exn1
25+
let exn2 = nullArgCheck (nameof exn2) exn2
2626
#endif
2727
let f (e: exn) =
2828
match e with

0 commit comments

Comments
 (0)