有条件的意外分号

时间:2012-04-09 16:28:23

标签: haskell cabal

我有以下代码行,当使用GHC编译时,它没有任何障碍:

addRDF c (Just (FILE))  = do
  (_:file:_) <- getArgs
  check <- doesFileExist file
  if check then do rdfG <- TLI.readFile file >>= (return . parseN3fromText)
                   case rdfG of (Left s)  -> putStrLn s
                                (Right g) -> storeRDF c g
  else do putStrLn "Specified files does not exist"

但是当我在cabal构建过程中运行它时,它会转储出以下错误。

Repository/Adder.hs:46:35:
    Unexpected semi-colons in conditional:
    if check then do { rdfG <- TLI.readFile file
                             >>=
                               (return . parseN3fromText);
                       case rdfG of {
                         (Left s) -> putStrLn s
                         (Right g) -> storeRDF c g } }; else do { putStrLn
                                                                    "Specified files does not exist" }
Perhaps you meant to use -XDoAndIfThenElse?

我可以在错误中看到额外的分号,但我不明白它的来源。

这是我的cabal配置文件:

cabal-version: >= 1.2
build-type: Simple

library
  build-depends:
    base,
    containers,
    HTTP >= 4000.2.2,
    directory >= 1.1.0.0,
    text >= 0.11.1.13,
    swish >= 0.6.5.2
  exposed-modules: Repository.Adder, Repository.Configuration
  ghc-options: -Wall

executable repository-add
  main-is: repository-add.hs
  build-depends:
    MissingH,
    swish >= 0.6.5.2,
    split >= 0.1.4.2
  ghc-options: -Wall

更新

正确缩进if

addRDF c (Just (FILE))  = do (_:file:_) <- getArgs
  check <- doesFileExist file
  if check
  then do rdfG <- TLI.readFile file >>= (return . parseN3fromText)
          case rdfG of (Left s)  -> putStrLn s
                       (Right g) -> storeRDF c g
  else do putStrLn "Specified files does not exist"

我现在在check之后得到一个分号:

Repository/Adder.hs:46:35:
    Unexpected semi-colons in conditional:
        if check; then do { rdfG <- TLI.readFile file
                                  >>=
                                    (return . parseN3fromText);
                            case rdfG of {
                              (Left s) -> putStrLn s
                              (Right g) -> storeRDF c g } }; else do { putStrLn
                                                                         "Specified files does not exist" }
    Perhaps you meant to use -XDoAndIfThenElse?

1 个答案:

答案 0 :(得分:15)

您的缩进是不正确的,但是当您使用原始GHC编译器时它会起作用,因为它会自动打开错误消息(DoAndIfThenElse)中提到的语法扩展。

在Cabal中,您必须指定手动使用的语言扩展名,位于代码文件的顶部或Cabal文件中;否则,编译器将不会启用它们。

if-clauses缩进的一个正确版本是这样的:

if check
  then do
    rdfG <- TLI.readFile file >>= (return . parseN3fromText)
    case rdfG of
      (Left s)  -> putStrLn s
      (Right g) -> storeRDF c g
  else putStrLn "Specified files does not exist"

您必须将then部分和else部分保持在比它们所属的块更深的缩进级别。

相关问题