Skip to content

Commit b48d3fe

Browse files
xperiandriclaude
andcommitted
refactor: build the messages of the Raise module with interpolated strings
The exceptions no longer go through String.Format with a format string and an array of boxed values. Each message is an interpolated string, and the helper that formatted the array is gone. A script called every function with 31 argument combinations in six cultures (invariant, en-US, de-DE, sv-SE, ar-SA and fa-IR) before and after the change: the 186 messages are identical, the minus sign of sv-SE included, because a hole without a format specifier formats with the current culture as String.Format does, as the messages of FSharp.Core do. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
1 parent 0955741 commit b48d3fe

1 file changed

Lines changed: 24 additions & 33 deletions

File tree

  • src/FSharp.Collections.Immutable/Helper

‎src/FSharp.Collections.Immutable/Helper/Raise.fs‎

Lines changed: 24 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -9,65 +9,56 @@ module internal FSharp.Collections.Immutable.Raise
99
open System
1010
open System.Collections.Generic
1111

12-
let inline private invalidArgFormat (arg : string) (format : string) (paramArray : obj array) =
13-
raise (ArgumentException (String.Format (format, paramArray), arg))
14-
1512
/// The arrays of a function of two arrays have different lengths.
1613
let invalidArgDifferentArrayLength (arg1 : string) (len1 : int) (arg2 : string) (len2 : int) : 'T =
17-
invalidArgFormat arg1 "{0}\n{1}.Length = {2}, {3}.Length = {4}" [|
18-
ErrorStrings.ArraysHadDifferentLengths
19-
arg1
20-
len1
21-
arg2
22-
len2
23-
|]
14+
let message =
15+
$"{ErrorStrings.ArraysHadDifferentLengths}\n{arg1}.Length = {len1}, {arg2}.Length = {len2}"
16+
17+
raise (ArgumentException (message, arg1))
2418

2519
/// The lists of a function of two lists have different lengths; `diff` is how much shorter `arg1` is.
2620
let invalidArgDifferentListLength (arg1 : string) (arg2 : string) (diff : int) : 'T =
27-
invalidArgFormat arg1 "{0}\n{1} is {2} {3} shorter than {4}" [|
28-
ErrorStrings.ListsHadDifferentLengths
29-
arg1
30-
diff
31-
(if diff = 1 then "element" else "elements")
32-
arg2
33-
|]
21+
let elements = if diff = 1 then "element" else "elements"
22+
23+
let message =
24+
$"{ErrorStrings.ListsHadDifferentLengths}\n{arg1} is {diff} {elements} shorter than {arg2}"
25+
26+
raise (ArgumentException (message, arg1))
3427

3528
/// The arrays of a function of three arrays have different lengths.
3629
let invalidArg3ArraysDifferent (arg1 : string) (arg2 : string) (arg3 : string) (len1 : int) (len2 : int) (len3 : int) : 'T =
37-
invalidArgFormat
38-
(String.Concat [| arg1; ", "; arg2; ", "; arg3 |])
39-
"{0}\n {1}.Length = {2}, {3}.Length = {4}, {5}.Length = {6}"
40-
[| ErrorStrings.ArraysHadDifferentLengths; arg1; len1; arg2; len2; arg3; len3 |]
30+
let message =
31+
$"{ErrorStrings.ArraysHadDifferentLengths}\n {arg1}.Length = {len1}, {arg2}.Length = {len2}, {arg3}.Length = {len3}"
32+
33+
raise (ArgumentException (message, $"{arg1}, {arg2}, {arg3}"))
4134

4235
/// The lists of a function of three lists have different lengths.
4336
let invalidArg3ListsDifferent (arg1 : string) (arg2 : string) (arg3 : string) (len1 : int) (len2 : int) (len3 : int) : 'T =
44-
invalidArgFormat
45-
(String.Concat [| arg1; ", "; arg2; ", "; arg3 |])
46-
"{0}\n {1}.Length = {2}, {3}.Length = {4}, {5}.Length = {6}"
47-
[| ErrorStrings.ListsHadDifferentLengths; arg1; len1; arg2; len2; arg3; len3 |]
37+
let message =
38+
$"{ErrorStrings.ListsHadDifferentLengths}\n {arg1}.Length = {len1}, {arg2}.Length = {len2}, {arg3}.Length = {len3}"
39+
40+
raise (ArgumentException (message, $"{arg1}, {arg2}, {arg3}"))
4841

4942
/// A count or an index is negative.
5043
let invalidArgInputMustBeNonNegative (arg : string) (count : int) : 'T =
51-
invalidArgFormat arg "{0}\n{1} = {2}" [| ErrorStrings.InputMustBeNonNegative; arg; count |]
44+
raise (ArgumentException ($"{ErrorStrings.InputMustBeNonNegative}\n{arg} = {count}", arg))
5245

5346
/// A size is not positive.
5447
let invalidArgInputMustBePositive (arg : string) (count : int) : 'T =
55-
invalidArgFormat arg "{0}\n{1} = {2}" [| ErrorStrings.InputMustBePositive; arg; count |]
48+
raise (ArgumentException ($"{ErrorStrings.InputMustBePositive}\n{arg} = {count}", arg))
5649

5750
/// An index or a count is beyond a bound.
5851
let invalidArgOutOfRange (arg : string) (index : int) (text : string) (bound : int) : 'T =
59-
let message =
60-
String.Format ("{0}\n{1} = {2}, {3} = {4}", [| box ErrorStrings.OutOfRange; arg; index; text; bound |])
52+
let message = $"{ErrorStrings.OutOfRange}\n{arg} = {index}, {text} = {bound}"
6153

6254
raise (ArgumentOutOfRangeException (arg, message))
6355

6456
/// A list is shorter than an index.
6557
let invalidOpListNotEnoughElements (index : int) : 'T =
58+
let elements = if index = 1 then "element" else "elements"
59+
6660
let message =
67-
String.Format (
68-
"{0}\nThe list was {1} {2} shorter than the index",
69-
[| box ErrorStrings.NotEnoughElements; index; (if index = 1 then "element" else "elements") |]
70-
)
61+
$"{ErrorStrings.NotEnoughElements}\nThe list was {index} {elements} shorter than the index"
7162

7263
raise (InvalidOperationException message)
7364

0 commit comments

Comments
 (0)