With haskell's turtle library, how to extract filename as String from FilePath?

一曲冷凌霜 提交于 2019-12-11 11:56:09

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!