packages feed

smuggler2-0.3.4.2: test/tests/Imported/TypeFam.hs

{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE OverloadedStrings     #-}
{-# LANGUAGE TypeFamilies          #-}

-- http://www.mchaver.com/posts/2017-06-21-type-families.html

module Imported.TypeFam where

import qualified Data.ByteString.Char8 as BS
import           Data.ByteString (ByteString)
import           Data.Monoid ((<>))
import qualified Data.Text as T
import           Data.Text (Text)

class Concat a b where
  type ConcatTy a b -- the declared type will replace this type synonym
  cat :: a -> b -> ConcatTy a b

instance Concat Text String where
  type ConcatTy Text String = Text
  cat x y = x <> (T.pack y)

instance Concat String Text where
  type ConcatTy String Text = Text
  cat x y = (T.pack x) <> y

instance Concat String ByteString where
  type ConcatTy String ByteString = String
  cat x y = x ++ (BS.unpack y)