What does new Array(x)
do? Trick question. It does different things depending on whether x
is an integer or something else.
I wanted to replicate this elegant functionality in Haskell: behold.
{-# LANGUAGE FlexibleContexts, ScopedTypeVariables, TypeApplications, TypeFamilies #-} | |
module EvilList where | |
import Data.Typeable | |
type family EvilList a where | |
EvilList Int = [()] | |
EvilList a = [a] | |
-- | Emulating the elegance of JavaScript's @new Array@. | |
-- | |
-- >>> evilList (5 :: Int) | |
-- [(),(),(),(),()] | |
-- | |
-- >>> evilList "hello" | |
-- ["hello"] | |
evilList :: forall a. (Typeable a, Typeable (EvilList a)) => a -> EvilList a | |
evilList a | |
| Just Refl <- eqT @a @Int = replicate a () | |
| Just Refl <- eqT @[a] @(EvilList a) = [a] | |
| otherwise = undefined |
Now make it accept variable number of arguments.
:(