So, I've been banging my head against the wall for at least an hour now. Here's my problem:
One of my functions returns [(Text, Text)]
.
Now I want to populate Document in Data.Bson
using this list of tuples.
With the extension OverloadedStrings
turned on, and with Data.Text
imported as well as Data.Bson
, I do (this part is edited from original post, which I pasted a debug version of the code which actually doesn't use the accumulator):
tuplesToBSON :: [(Text, Text)] -> Document
tuplesToBSON xs = L.foldr (\v acc -> merge [fst v =: snd v] acc) [] xs
Where merge
is from the Data.Bson package, as is =:
.
No luck. It complains: Couldn't match expected type Label
with actual type Text
. (below is also an edit for clarity) If I try:
tuplesToBSON xs = L.foldr (\v acc -> merge [(fst v) =: (String (snd v))] acc ) [] xs
where this unfortunately named String
is actually a constructor (I think...) from the Bson package for a data type called "Value". http://hackage.haskell.org/packages/archive/bson/0.2.1/doc/html/src/Data-Bson.html
This still doesn't work--but a different error message:
No instance for (Val Value) arising from a use of `=:'
Possible fix: add an instance declaration for (Val Value)
In the expression: (fst v) =: (String (snd v))
In the first argument of `merge', namely `[(fst v) =: (String (snd v))]'
In the expression: merge [(fst v) =: (String (snd v))] acc
as well as:
Couldn't match expected type `Label' with actual type `Text'
Expected type: [(Label, text-0.11.2.0:Data.Text.Internal.Text)] Actual type: [(Text, Text)]
In the third argument of `L.foldr', namely `xs'
In the expression: L.foldr (\ v acc -> merge [(fst v) =: (String (snd v))] acc) [] xs
I know that Value is a data type from the Bson package, but I really don't know what Val
is.
===== START of now IRRELEVANT PORTION =====
And as one of the answers below pointed out, in the original post, I had :: String
in my code "casting" a Text
to a String
as in the Prelude. I actually thought :: String
was referring to the one mentioned in the Bson package, but I was completely wrong anyway, because (I think) String
is actually a constructor.
I suspect somehow the GHC type inference is confused somehow, because the following works fine:
testDocument :: Document
testDocument = [(fst ("asdf", "awef") :: Label) =: ("asdf" :: String)]
Edit: This works but must be because Bson does some proper type handling automatically because after included the line import Prelude hiding (String)
it gives me an error as it should.
So I've finally cleaned up the question. Sorry for the previous messy version--I was quite frustrated.
acc
on the floor. Maybe you meant to use((fst v) := (snd v)) : acc
instead of just returning a 1-element list? – Lily Ballard