@@ -111,26 +111,47 @@ type TryParse =
111
111
static member TryParse ( _ : string , _ : TryParse ) = fun x -> Some x : option< string>
112
112
static member TryParse ( _ : StringBuilder , _ : TryParse ) = fun x -> Some ( new StringBuilder ( x: string)) : option< StringBuilder>
113
113
#if ! FABLE_ COMPILER
114
- static member TryParse ( _ : DateTime , _ : TryParse ) = fun ( x : string ) -> DateTime.TryParseExact ( x, [| " yyyy-MM-ddTHH:mm:ss.fffZ" ; " yyyy-MM-ddTHH:mm:ssZ" |], null , DateTimeStyles.RoundtripKind) |> tupleToOption : option< DateTime>
115
- static member TryParse ( _ : DateTimeOffset , _ : TryParse ) = fun ( x : string ) -> DateTimeOffset.TryParseExact ( x, [| " yyyy-MM-ddTHH:mm:ss.fffK" ; " yyyy-MM-ddTHH:mm:ssK" |], null , DateTimeStyles.RoundtripKind) |> tupleToOption : option< DateTimeOffset>
114
+
115
+ static member TryParse ( _ : DateTime , _ : TryParse ) = fun ( x : string ) ->
116
+ match DateTime.TryParseExact ( x, [| " yyyy-MM-ddTHH:mm:ss.fffZ" ; " yyyy-MM-ddTHH:mm:ssZ" |], null , DateTimeStyles.RoundtripKind) with
117
+ | true , x -> Some x
118
+ | _ ->
119
+ match DateTime.TryParse ( x, CultureInfo.InvariantCulture, DateTimeStyles.None) with
120
+ | true , x -> Some x
121
+ | _ -> None
122
+
123
+ static member TryParse ( _ : DateTimeOffset , _ : TryParse ) = fun ( x : string ) ->
124
+ match DateTimeOffset.TryParseExact ( x, [| " yyyy-MM-ddTHH:mm:ss.fffK" ; " yyyy-MM-ddTHH:mm:ssK" |], null , DateTimeStyles.AssumeUniversal) with
125
+ | true , x -> Some x
126
+ | _ ->
127
+ match DateTimeOffset.TryParse ( x, CultureInfo.InvariantCulture, DateTimeStyles.None) with
128
+ | true , x -> Some x
129
+ | _ -> None
116
130
#endif
117
131
118
132
static member inline Invoke ( value : string ) =
119
133
let inline call_2 ( a : ^a , b : ^b ) = (( ^a or ^b ) : ( static member TryParse : _*_ -> _) b, a)
120
134
let inline call ( a : 'a ) = fun ( x : 'x ) -> call_ 2 ( a, Unchecked.defaultof< 'r>) x : 'r option
121
135
call Unchecked.defaultof< TryParse> value
122
136
123
- type TryParse with
124
- static member inline TryParse ( _ : 'R , _ : Default2 ) = fun x ->
137
+ /// The F# signature
138
+ static member inline InvokeOnInstance ( value : string ) = ( ^R : ( static member TryParse : string -> 'R option) value)
139
+
140
+ /// The .Net signature
141
+ static member inline InvokeOnConvention ( value : string ) =
125
142
let mutable r = Unchecked.defaultof< ^ R>
126
- if ( ^R : ( static member TryParse : _ * _ -> _) ( x, & r)) then Some r else None
143
+ if ( ^R : ( static member TryParse : _ * _ -> _) ( value, & r)) then Some r else None
144
+
145
+ #if NET7_ 0
146
+ /// IParsable<'T>
147
+ static member InvokeOnInterface < 'T when 'T :> IParsable < 'T >> ( value : string ) =
148
+ let mutable r = Unchecked.defaultof< 'T>
149
+ if ( 'T.TryParse( value, CultureInfo.InvariantCulture, & r)) then Some r else None
150
+ #endif
127
151
128
- static member inline TryParse ( _ : ^t when ^t : null and ^t : struct , _ : Default1 ) = id
129
- static member inline TryParse ( _ : 'R , _ : Default1 ) = fun x -> ( ^R : ( static member TryParse : string -> 'R option) x)
130
152
131
153
type Parse =
132
154
inherit Default1
133
- static member inline Parse ( _ : ^R , _ : Default1 ) = fun ( x : string ) -> ( ^R : ( static member Parse : _ -> ^R ) x)
134
155
static member inline Parse ( _ : ^R , _ : Parse ) = fun ( x : string ) -> ( ^R : ( static member Parse : _ * _ -> ^R ) ( x, CultureInfo.InvariantCulture))
135
156
136
157
static member inline Parse ( _ : 'T when 'T : enum < _ >, _ : Parse ) = fun ( x : string ) ->
@@ -139,6 +160,16 @@ type Parse =
139
160
| _ -> invalidArg " value" ( " Requested value '" + x + " ' was not found." )
140
161
) : 'enum
141
162
163
+ #if ! FABLE_ COMPILER
164
+ static member Parse ( _ : DateTime , _ : Parse ) = fun ( x : string ) ->
165
+ match DateTime.TryParseExact ( x, [| " yyyy-MM-ddTHH:mm:ss.fffZ" ; " yyyy-MM-ddTHH:mm:ssZ" |], null , DateTimeStyles.RoundtripKind) with
166
+ | true , x -> x
167
+ | _ -> DateTime.Parse ( x, CultureInfo.InvariantCulture)
168
+
169
+ static member Parse ( _ : DateTimeOffset , _ : Parse ) = fun ( x : string ) ->
170
+ try DateTimeOffset.ParseExact ( x, [| " yyyy-MM-ddTHH:mm:ss.fffK" ; " yyyy-MM-ddTHH:mm:ssK" |], null , DateTimeStyles.AssumeUniversal)
171
+ with _ -> DateTimeOffset.Parse ( x, CultureInfo.InvariantCulture)
172
+ #endif
142
173
143
174
static member Parse ( _ : bool , _ : Parse ) = fun ( x : string ) -> Boolean.Parse ( x)
144
175
@@ -151,4 +182,46 @@ type Parse =
151
182
let inline call ( a : 'a ) = fun ( x : 'x ) -> call_ 2 ( a, Unchecked.defaultof< 'r>) x : 'r
152
183
call Unchecked.defaultof< Parse> value
153
184
185
+ static member inline InvokeOnInstance ( value : string ) = ( ^R : ( static member Parse : _ -> ^R ) value)
186
+
187
+
188
+ type Parse with
189
+
190
+ static member inline Parse ( _ : ^R , _ : Default4 ) = fun ( value : string ) ->
191
+ match TryParse.InvokeOnConvention value with
192
+ | Some x -> x : ^ R
193
+ | None -> invalidArg " value" ( " Error parsing value '" + value + " '." )
194
+
195
+ static member inline Parse ( _ : ^R , _ : Default3 ) = fun ( value : string ) ->
196
+ match TryParse.InvokeOnInstance value with
197
+ | Some x -> x : ^ R
198
+ | None -> invalidArg " value" ( " Error parsing value '" + value + " '." )
199
+
200
+ static member inline Parse ( _ : ^R , _ : Default2 ) : string -> ^R = Parse.InvokeOnInstance
201
+
202
+ #if NET7_ 0
203
+ static member Parse < 'T when 'T :> IParsable < 'T >> ( _ : 'T , _ : Default1 ) = fun ( x : string ) -> 'T.Parse ( x, CultureInfo.InvariantCulture)
204
+ static member inline Parse ( _ : ^t when ^t : null and ^t : struct , _ : Default1 ) = id
205
+ #else
206
+ static member inline Parse ( _ : ^t when ^t : null and ^t : struct , _ : Default2 ) = id
207
+ #endif
208
+
209
+ type TryParse with
210
+
211
+ static member inline TryParse ( _ : 'R , _ : Default4 ) : string -> 'R option = fun ( value : string ) ->
212
+ try Some ( Parse.InvokeOnInstance value) with
213
+ | :? ArgumentNullException | :? FormatException -> None
214
+ | _ -> reraise ()
215
+
216
+ static member inline TryParse ( _ : 'R , _ : Default3 ) : string -> 'R option = TryParse.InvokeOnConvention
217
+
218
+ static member inline TryParse ( _ : 'R , _ : Default2 ) : string -> 'R option = TryParse.InvokeOnInstance
219
+
220
+ #if NET7_ 0
221
+ static member inline TryParse ( _ : 'R , _ : Default1 ) : string -> 'R option = TryParse.InvokeOnInterface
222
+ static member inline TryParse ( _ : ^t when ^t : null and ^t : struct , _ : Default1 ) = id
223
+ #else
224
+ static member inline TryParse ( _ : ^t when ^t : null and ^t : struct , _ : Default2 ) = id
225
+ #endif
226
+
154
227
#endif
0 commit comments