Skip to content

Instantly share code, notes, and snippets.

@pasberth
Created May 26, 2013 10:59
Show Gist options
  • Save pasberth/5652444 to your computer and use it in GitHub Desktop.
Save pasberth/5652444 to your computer and use it in GitHub Desktop.
暗黙の型変換
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TypeSynonymInstances #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE FlexibleContexts #-}
module Control.Implicit where
import qualified Data.Text as T
import qualified Data.ByteString.Char8 as BS
import qualified Data.ByteString.Lazy.Char8 as BSL
class Implicit a b where
convert :: b -> a
implicit :: Implicit a b => (a -> c) -> b -> c
implicit = (. convert)
instance Implicit a a where
convert = id
instance Implicit String T.Text where
convert = T.unpack
instance Implicit T.Text String where
convert = T.pack
instance Implicit String BS.ByteString where
convert = BS.unpack
instance Implicit BS.ByteString String where
convert = BS.pack
instance Implicit String BSL.ByteString where
convert = BSL.unpack
instance Implicit BSL.ByteString String where
convert = BSL.pack
-- implicitPutStr :: Implicit String a => a -> IO ()
-- implicitPutStr = implicit putStr
--
-- implicitPutStr "hello" -- OK
-- implicitPutStr $ BS.pack "hello" -- OK
-- implicitPutStr $ T.pack "hello" -- OK
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment