I am trying to do this
data Foo a = Foo a
data FooWrapper = FooWrapper (forall a. Foo a)
foo = [FooWrapper (Foo 0), FooWrapper (Foo "")]
But there is an error
Could not match type
Intwith type
a0
Existential types don't quite work the same in PureScript as they do in Haskell, so usually we use the purescript-exists library for this kind of thing.
The equivalent using Exists would be:
import Data.Exists (Exists(), mkExists)
data Foo a = Foo a
data FooWrapper = FooWrapper (Exists Foo)
foo = [FooWrapper (mkExists (Foo 0)), FooWrapper (mkExists (Foo ""))]
I suppose in this case you probably don't need FooWrapper at all and could just have an array of Exists Foo.
Exists? data FooWrapper = FooWrapper (forall a. Show a => Foo a) For example, how can I get ["0", ""] :: Array String from [(mkExists (Foo 0)), (mkExists (Foo ""))]?forall, e.g. type SomeShow = forall r. (forall a. Show a => a -> r) -> r.getStrings stackoverflow.com/questions/36006483/… ?I wanted to know how Phil Freeman's suggested approach works, so I gave it a try. This is a working example of storing a type class instance with its value by using a rank-n type.
module Example where
import Prelude (class Show, Unit, discard, pure, show, unit, ($))
import Effect (Effect)
import Effect.Console (log)
newtype Showable = Showable (forall r. (forall a. Show a => a -> r) -> r)
instance showShowable :: Show Showable where
show (Showable f) = f show
mkShowable :: forall s . Show s => s -> Showable
mkShowable s = Showable (\f -> f s)
showables :: Array Showable
showables = [mkShowable 1, mkShowable "a string", mkShowable { foo : "bar" } ]
main :: Effect Unit
main = do
log $ show showables
pure unit
The newtype is not really necessary for storing, but i wanted to create an instance of Show for the type itself .
data Foo a b = Foo a bfoo1 :: Foo Int Stringfoo2 :: Foo Int IntI what to create a function that could work with array of Foo even if their second parameters have the different typesdoSomething [foo1, foo2]because the function only use first parameter.