Is there an existing data type for something like StrMap (without the type variable)

29 views
Skip to first unread message

rvdalen

unread,
Nov 15, 2017, 11:44:59 AM11/15/17
to purescript
Hi everyone,

Just before implementing my own naive solution, I just want to check if some solution already exists
for my current problem.

I need a data type similar to `StrMap a`, but where I can insert a value of any type (and different types)
for the same map.  So I basically need a map that has String keys, but can contain values of different types.

I will never need to read back the values in the map in PureScript code.  I eventually hand it off to the Mithril JavaScript framework
which expects a JavaScript object.

Is there something that already fits my needs or will I have to roll my own data type for this?

Regards
--Rouan

Marcin Szamotulski

unread,
Nov 15, 2017, 11:57:51 AM11/15/17
to pures...@googlegroups.com
Hi Rouan,

Even if you don't need to read the values, you might want to type check
that the values have expected types. That would require writing an
interface for creating such records. I only briefly looked at the
Mithril framework, but it seems you could adopt the same strategy as
in react bindings.

Otherwise you could use extensible records and unsafeCoerce:
```
data StrMap'
veryUnsafeStrMap' :: forall r. {|r} -> StrMap'
veryUnsafeStrMap' = unsafeCoerce
```

But writing proper bindings will pay of in the long run.

Regards,
Marcin
> --
> You received this message because you are subscribed to the Google Groups "purescript" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to purescript+...@googlegroups.com.
> Visit this group at https://groups.google.com/group/purescript.
> For more options, visit https://groups.google.com/d/optout.

signature.asc

Harry Garrood

unread,
Nov 15, 2017, 1:18:33 PM11/15/17
to pures...@googlegroups.com
Hi Rouan,

That sounds like a job for purescript-options:
https://github.com/purescript-contrib/purescript-options

It's a little under-documented currently but I have a PR open that
addresses this, so you could read my docs there:
https://github.com/purescript-contrib/purescript-options/pull/20

Regards
Harry

rvdalen

unread,
Nov 16, 2017, 5:45:47 AM11/16/17
to purescript
Thanks Marcin and Harry,

I had a look at purescript-options and it has some similarities to what I need,
but not enough to make we want to use it.

So far I am thinking about creating my own StrMap_ (for lack of a better name)
which is basically the same as the normal StrMap, but you can only put values (of any type)
into it.  This seems like the best solution for what I need.

Before switching to StrMap, I had used {|r} record types, but i need to sometimes add my own fields to the record,
which is easier to do if I switch to some kind of Map data structure.

The other problem with purescript-options is I want something that I do not have to define an "interface" for.

The only thing I want is to be able to add key/value pairs to the map and send it off as a JS object.

Is this something that might be useful for other people as well?

Regards
--Rouan
Reply all
Reply to author
Forward
0 new messages