3

I need to execute the cp_r function from the Shelly library to copy a to b.

However,

import Shelly
main = do cp_r "a" "b"

yields

Couldn't match expected type `Shelly.FilePath'
            with actual type `[Char]'
In the first argument of `cp_r', namely `"a"'
In the expression: cp_r "a" "b"
In an equation for `it': it = cp_r "a" "b"

for both the first and the second argument to cp_r.

How can I use String (FilePath is defined as String on any platform I know of) as argument to cp_r?

Note: This question intentionally does not show any research effort as it was answered Q&A-Style.

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120

2 Answers2

2

I agree with @Uli Kohler - here is an example of such usage:

https://github.com/haroldcarr/make-mp3-copies/blob/master/MakeMP3Copies.hs

haroldcarr
  • 1,523
  • 15
  • 17
1

For a detailed and official description, see the convert between Text and FilePath section on Hackage.

Let's first see how to do it with Text:

{-# LANGUAGE OverloadedStrings #-}
import Shelly

cp_r (fromText "a") (fromText "b")

From here on, we can just use Text.pack to apply this method to strings

{-# LANGUAGE OverloadedStrings #-}
import Shelly
import Data.Text

cp_r (fromText $ pack "a") (fromText $ pack "b")

Note that in case you also need to use the FilePath from Prelude in a module, you need to use

import Shelly hiding (FilePath)

to avoid conflicts (alternatively, you could use Prelude.FilePath and Shelly.FilePath).

Uli Köhler
  • 13,012
  • 16
  • 70
  • 120