How can I create an array with polymorphic data?

南楼画角 提交于 2019-12-12 01:16:08

问题


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

Int

with type

a0

回答1:


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.



来源:https://stackoverflow.com/questions/35990022/how-can-i-create-an-array-with-polymorphic-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!