Skip to content

Commit 644ba00

Browse files
committed
good fixed witch instances
1 parent 8109831 commit 644ba00

File tree

1 file changed

+20
-12
lines changed
  • pub/functora-witch/source/library/Functora/Witch

1 file changed

+20
-12
lines changed

pub/functora-witch/source/library/Functora/Witch/Instances.hs

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Functora.Witch.Instances where
1111

1212
import qualified Control.Exception as Exception
1313
import qualified Control.Monad as Monad
14+
import qualified Data.Bifunctor as Bifunctor
1415
import qualified Data.Bits as Bits
1516
import qualified Data.ByteString as ByteString
1617
import qualified Data.ByteString.Char8 as Char8
@@ -936,24 +937,31 @@ instance (Fixed.HasResolution a) => TryFrom.TryFrom Rational (Fixed.Fixed a) whe
936937

937938
-- Fixed
938939

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
947944

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
952950

953951
-- | Uses 'toRational'.
954952
instance (Fixed.HasResolution a) => From.From (Fixed.Fixed a) Rational where
955953
from = toRational
956954

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+
957965
-- Complex
958966

959967
-- | Uses '(Complex.:+)' with an imaginary part of 0.

0 commit comments

Comments
 (0)