Skip to content

Instantly share code, notes, and snippets.

@acple
Created September 4, 2018 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save acple/5a7c89e868238dc25700f0b73f572cab to your computer and use it in GitHub Desktop.
Save acple/5a7c89e868238dc25700f0b73f572cab to your computer and use it in GitHub Desktop.
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