Quick actions

cmd+k|ctrl+k

Navigation

Languages

Number Literals

Snippet info

Language

Haskell

Visibility

public

Author

homam

Created

2019-06-25T21:39:23Z

Updated

2019-06-25T21:39:23Z

-- https://bitemyapp.com/blog/instance-local-fundeps/

{-# LANGUAGE TypeFamilies, FlexibleInstances #-}

data Bytes =
    B
  | KB
  | MB

instance (b ~ Double) => Num (Bytes -> b) where
  fromInteger i B =
    fromInteger i
  fromInteger i KB =
    fromInteger $ i * 1024
  fromInteger i MB =
    fromInteger $ i * 1024 * 1024

instance (b ~ Double) => Fractional (Bytes -> b) where
  fromRational r B =
    fromRational r
  fromRational r KB =
    fromRational $ r * 1024
  fromRational r MB =
    fromRational $ r * 1024 * 1024


main = print $ 100 KB
INFO