Why can one sometimes get “No instance for CSV Tex

2019-08-14 21:55发布

问题:

My function is relatively simple and using fairly standard libraries:

{-# LANGUAGE OverloadedStrings #-}

module Foo where

import           Conduit
import           Data.CSV.Conduit

textToCSV :: Text -> Vector (MapRow Text)
textToCSV txt = replicateC 1 txt
  .| intoCSV defCSVSettings
  .| sinkVector & runConduitPure & runIdentity

I'd like to understand why I'm getting these type errors, and a solution would be great too:

    * No instance for (CSV Text (Vector (MapRow Text)))
        arising from a use of `intoCSV'
    * In the first argument of `(.|)', namely `intoCSV defCSVSettings'
      In the second argument of `(.|)', namely
        `intoCSV defCSVSettings .| sinkVector'
      In the first argument of `(&)', namely
        `replicateC 1 txt .| intoCSV defCSVSettings .| sinkVector'
    |
156 |   .| intoCSV defCSVSettings
    |      ^^^^^^^^^^^^^^^^^^^^^^


    * No instance for (Data.Vector.Generic.Base.Vector
                         Identity (Vector (MapRow Text)))
        arising from a use of `sinkVector'
    * In the second argument of `(.|)', namely `sinkVector'
      In the second argument of `(.|)', namely
        `intoCSV defCSVSettings .| sinkVector'
      In the first argument of `(&)', namely
        `replicateC 1 txt .| intoCSV defCSVSettings .| sinkVector'
    |
157 |   .| sinkVector & runConduitPure & runIdentity
    |      ^^^^^^^^^^

Certainly, I can agree that an instance of CSV Text Text doesn't exist. But compared to what I'm doing, the typical usage using sourceFile seems to have a very similar usage. I'm just as confused about the second error regarding vectors.

Possible workaround:

Not really an answer to the question, but a workaround I found (that is also more succinct) was to replace my textToCSV function and several other auxiliary functions in the construction of my processCSV conduit:

processCSV :: (MonadResource m, MonadThrow m, PrimMonad m) =>
  ConduitT FilePath Void m (Vector (Vector (MapRow Text)))
processCSV = mapMC (readCSVFile defCSVSettings) .| sinkVector