Created
September 4, 2018 12:47
-
-
Save acple/5a7c89e868238dc25700f0b73f572cab to your computer and use it in GitHub Desktop.
This file contains 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
module Main where | |
import Prelude | |
import Data.Exists (Exists, mkExists, runExists) | |
import Data.Foldable (traverse_) | |
import Data.Maybe (Maybe(..)) | |
import Effect (Effect) | |
import Effect.Class.Console (log) | |
---------------------------------------------------------------- | |
-- 任意のaに対して値とshowをセットで扱う | |
newtype ShowBox' a = SB { value :: a, show :: a -> String } | |
-- aがどこかにきえた! | |
newtype ShowBox = ShowBox (Exists ShowBox') | |
instance showSB :: Show ShowBox where | |
show (ShowBox sb) = sb # runExists \(SB { show: sh, value }) -> sh value -- なかのshowにvalueをぶつけるだけ | |
-- コンストラクタのような | |
mkShowBox :: forall a. Show a => a -> ShowBox | |
mkShowBox = ShowBox <<< mkExists <<< SB <<< { value: _, show } | |
main :: Effect Unit | |
main = do | |
let hetero = [ mkShowBox 1, mkShowBox "abc", mkShowBox [ unit, unit ], mkShowBox { x: Just 123, y: 1000.0 } ] | |
traverse_ (log <<< show) hetero |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment