chore: change the signature of the Convert and MustConvert#78
Conversation
These functions were accepting `any` as argument, which was not really helpful. Some types were not supported and the users had to figure out by himself at the runtime. The unsupported types are now reported at the compilation time. This is a breaking change, but it's the whole point of this commit. BREAKING CHANGE: a minor one but still breaking one.
|
Thank you @c4rlo for inspiring me this change |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## main #78 +/- ##
===========================================
- Coverage 100.00% 98.78% -1.22%
===========================================
Files 3 3
Lines 247 247
===========================================
- Hits 247 244 -3
- Misses 0 3 +3 ☔ View full report in Codecov by Sentry. |
|
The regression in the coverage is expected https://app.codecov.io/gh/ccoVeille/go-safecast/pull/78/blob/conversion.go It's now impossible to pass something that is not supported, and that's the whole idea of the refactoring. Because of the type check switch, and the fact each type needs to be handled in its own case, I didn't find a way (yet) to provide code that could avoid this regression |
| func Convert[NumOut Number](orig any) (converted NumOut, err error) { | ||
| switch v := orig.(type) { | ||
| func Convert[NumOut Number, NumIn Input](orig NumIn) (converted NumOut, err error) { | ||
| switch v := any(orig).(type) { |
There was a problem hiding this comment.
What if orig is of a type A declared as type A int? In that case, A satisfies Input so it compiles, but I believe this switch statement won't find a match and hence this function returns an error.
There was a problem hiding this comment.
Yes, it's the first step of a refactoring.
Here I'm only applying a part of the refactoring that need to be done.
I'm only addressing the "the function accepts any" issue that you raised a few weeks ago.
I still have to deal with "type alias are not working" issue. See #72
I have code ready locally.
But I wanted this PR first.

These functions were accepting
anyas argument, which was not really helpful.Some types were not supported, and the users had to figure out by himself at the runtime.
The unsupported types are now reported at the compilation time.
This is a breaking change, but it's the whole point of this commit.
BREAKING CHANGE: a minor one but still breaking one.
Fixes #76