Skip to content

Commit 0191b86

Browse files
committed
T functions no perf penalty + pkg documentation of T funcs
1 parent 04fa9cc commit 0191b86

File tree

1 file changed

+40
-31
lines changed

1 file changed

+40
-31
lines changed

try/try.go

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ Or you might just want to change it later to error return:
6060
6161
Please see the documentation and examples of [Result], [Result1], and [Result2]
6262
types and their methods.
63+
64+
# try.T — Checking and Annotation
65+
66+
The try package offers functions [T], [T1], [T2], and [T3] to allow fast
67+
incremental code refactoring. For example, if you want to add an error check
68+
specific annotation to the error check already done with [To1]:
69+
70+
try.To1(io.Copy(w, r))
71+
72+
you can easily change it to [T1] and give extra message added to the error:
73+
74+
try.T1(io.Copy(w, r))("error during stream copy")
75+
76+
The T functions are offered mainly to allow faste feedback loop to play with the
77+
error messages and see what works the best.
6378
*/
6479
package try
6580

@@ -245,54 +260,48 @@ func IsNotEnabled(err error) bool {
245260
// T is similar as [To] but it let's you to annotate a possible error at place.
246261
//
247262
// try.T(f.Close)("annotations")
248-
func T(err error) func(fs string, a ...any) {
249-
return func(fs string, a ...any) {
250-
// NOTE if block cannot be refactored 'because it wouldn't inline
251-
// then this whole function!
252-
if err != nil {
253-
er := fmt.Errorf(fs+handler.WrapError, append(a[1:], err)...)
254-
panic(er)
263+
func T(err error) func(fs string) {
264+
return func(fs string) {
265+
if err == nil {
266+
return
255267
}
268+
panic(annotateErr(err, fs))
256269
}
257270
}
258271

259272
// T1 is similar as [To1] but it let's you to annotate a possible error at place.
260273
//
261274
// f := try.T1(os.Open("filename")("cannot open cfg file")
262-
func T1[T any](v T, err error) func(fs string, a ...any) T {
263-
return func(fs string, a ...any) T {
264-
// NOTE if block cannot be refactored 'because it wouldn't inline
265-
// then this whole function!
266-
if err != nil {
267-
er := fmt.Errorf(fs+handler.WrapError, append(a[1:], err)...)
268-
panic(er)
275+
func T1[T any](v T, err error) func(fs string) T {
276+
return func(fs string) T {
277+
if err == nil {
278+
return v
269279
}
270-
return v
280+
panic(annotateErr(err, fs))
271281
}
272282
}
273283

274284
// T2 is similar as [To2] but it let's you to annotate a possible error at place.
275-
func T2[T, U any](v T, u U, err error) func(fs string, a ...any) (T, U) {
276-
return func(fs string, a ...any) (T, U) {
277-
// NOTE if block cannot be refactored 'because it wouldn't inline
278-
// then this whole function!
279-
if err != nil {
280-
er := fmt.Errorf(fs+handler.WrapError, append(a[1:], err)...)
281-
panic(er)
285+
func T2[T, U any](v T, u U, err error) func(fs string) (T, U) {
286+
return func(fs string) (T, U) {
287+
if err == nil {
288+
return v, u
282289
}
283-
return v, u
290+
panic(annotateErr(err, fs))
284291
}
292+
293+
}
294+
295+
func annotateErr(err error, fs string) error {
296+
return fmt.Errorf(fs+handler.WrapError, err)
285297
}
286298

287299
// T3 is similar as [To3] but it let's you to annotate a possible error at place.
288-
func T3[T, U, V any](v1 T, v2 U, v3 V, err error) func(fs string, a ...any) (T, U, V) {
289-
return func(fs string, a ...any) (T, U, V) {
290-
// NOTE if block cannot be refactored 'because it wouldn't inline
291-
// then this whole function!
292-
if err != nil {
293-
er := fmt.Errorf(fs+handler.WrapError, append(a[1:], err)...)
294-
panic(er)
300+
func T3[T, U, V any](v1 T, v2 U, v3 V, err error) func(fs string) (T, U, V) {
301+
return func(fs string) (T, U, V) {
302+
if err == nil {
303+
return v1, v2, v3
295304
}
296-
return v1, v2, v3
305+
panic(annotateErr(err, fs))
297306
}
298307
}

0 commit comments

Comments
 (0)