@@ -11,6 +11,7 @@ module Functora.Witch.Instances where
11
11
12
12
import qualified Control.Exception as Exception
13
13
import qualified Control.Monad as Monad
14
+ import qualified Data.Bifunctor as Bifunctor
14
15
import qualified Data.Bits as Bits
15
16
import qualified Data.ByteString as ByteString
16
17
import qualified Data.ByteString.Char8 as Char8
@@ -936,24 +937,31 @@ instance (Fixed.HasResolution a) => TryFrom.TryFrom Rational (Fixed.Fixed a) whe
936
937
937
938
-- Fixed
938
939
939
- --
940
- -- TODO : update after fix https://github.com/tfausak/witch/issues/121
941
- --
942
-
943
- -- -- | Uses 'Fixed.MkFixed'. This means @from \@Integer \@Centi 2@ is @0.02@
944
- -- -- rather than @2.00@.
945
- -- instance From.From Integer (Fixed.Fixed a) where
946
- -- from = Fixed.MkFixed
940
+ -- | Uses 'fromInteger'. This means @from \@Integer \@Centi 2@ is @02.00@
941
+ -- rather than @0.02@.
942
+ instance (Fixed. HasResolution a ) => From. From Integer (Fixed. Fixed a ) where
943
+ from = fromInteger
947
944
948
- -- -- | Uses 'Fixed.MkFixed'. This means @from \@Centi \@Integer 3.00@ is @300@
949
- -- -- rather than @3@.
950
- -- instance From.From (Fixed.Fixed a) Integer where
951
- -- from (Fixed.MkFixed t) = t
945
+ -- | Converts via 'Rational' when there is no fractional part. This means
946
+ -- @tryFrom \@Centi \@Integer 2.00@ is @Right 2@ and
947
+ -- @tryFrom \@Centi \@Integer 0.02@ will fail.
948
+ instance (Fixed. HasResolution a ) => TryFrom. TryFrom (Fixed. Fixed a ) Integer where
949
+ tryFrom = Utility. eitherTryFrom $ TryFrom. tryFrom @ Rational . From. from
952
950
953
951
-- | Uses 'toRational'.
954
952
instance (Fixed. HasResolution a ) => From. From (Fixed. Fixed a ) Rational where
955
953
from = toRational
956
954
955
+ -- | Converts via 'Rational'.
956
+ instance
957
+ (Fixed. HasResolution s , Fixed. HasResolution t ) =>
958
+ TryFrom. TryFrom (Fixed. Fixed s ) (Fixed. Fixed t )
959
+ where
960
+ tryFrom s =
961
+ Bifunctor. first (Utility. withSource s)
962
+ . TryFrom. tryFrom @ Rational
963
+ $ From. from s
964
+
957
965
-- Complex
958
966
959
967
-- | Uses '(Complex.:+)' with an imaginary part of 0.
0 commit comments