Created
June 4, 2015 07:01
-
-
Save relrod/232cbc526f676ac25780 to your computer and use it in GitHub Desktop.
This is that flexibility thing that Scala folks always talk about, right? ;) (aka I finally decided to learn about TypeFamilies, then had some fun)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# LANGUAGE FlexibleInstances #-} | |
{-# LANGUAGE MultiParamTypeClasses #-} | |
{-# LANGUAGE TypeFamilies #-} | |
{-# LANGUAGE RebindableSyntax #-} | |
module Foo where | |
import Prelude hiding ((+), Num(..)) | |
import qualified Prelude as P | |
default (Integer) | |
class Add a b where | |
type AddThings a b | |
(+) :: a -> b -> AddThings a b | |
instance Add String Integer where | |
type AddThings String Integer = String | |
a + b = a ++ show b | |
instance Add Integer String where | |
type AddThings Integer String = String | |
a + b = show a ++ b | |
instance P.Num a => Add a a where | |
type AddThings a a = a | |
a + b = a P.+ b | |
fromInteger :: Integer -> Integer | |
fromInteger = id | |
-- λ> :set -XRebindableSyntax | |
-- λ> :l /tmp/foo.hs | |
-- | |
-- λ> 3 + 4 | |
-- 7 | |
-- | |
-- λ> 3 + "four" | |
-- "3four" | |
-- | |
-- λ> "three" + 4 | |
-- "three4" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment