Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.