问题
When using takeFileName I get a type error:
:t v
print v
:t takeFileName
takeFileName v
v :: FilePath
FilePath "/media/miguel/backup/backups"
takeFileName :: FilePath -> FilePath
Couldn't match type ‘Turtle.FilePath’ with ‘String’
Expected type: IHaskellSysIO.FilePath
Actual type: Turtle.FilePath
In the first argument of ‘takeFileName’, namely ‘v’
In the expression: takeFileName v
Is it because turtle's FilePath is different from prelude's FilePath ?
回答1:
Turtle still uses system-filepath
which has a customized "FilePath" type you can find here. Many other Haskell libraries would use a filepath library which just defines FilePath
as a synonym for a String
(type FilePath = String). This is the case here with IHaskell
.
So yes both FilePath types mismatch. Note that you can easily convert Turtle.FilePath
into a String
using show
(because the type has a Show instance). You can also convert it into a Text
using fp
from the Turtle.Format module.
system-filepath
is actually deprecated. There is an issue about this. Please read: https://github.com/Gabriel439/Haskell-Turtle-Library/issues/54
Hope it helps.
回答2:
As mentioned by in the comment by miguel.negrao, you need the system-filepath
library (deprecated - but I can't find any other solution).
import Turtle hiding (f
import Filesystem.Path.CurrentOS (encodeString, fromText)
let f = fromText $ "/test/abc.txt" :: Turtle.FilePath
print $ encodeString f
The above will output /test/abc.txt
.
来源:https://stackoverflow.com/questions/34659580/with-haskells-turtle-library-how-to-extract-filename-as-string-from-filepath