2016-08-25 39 views
2

在使用GHCi的Haskell中,有一種加載文件的方法,例如下面的文件,它允許您測試具有綁定的方法。Haskell:GHCi儘可能加載,而不是失敗或錯誤

使用案例:試圖測試我的模塊的一部分,而其餘部分的骨架代碼。 (爲不具有XY問題)

module X (methodA, methodB, methodC) where 

methodA :: String->String 
methodA name = "Hello " ++ name 

methodB :: Int -> String 

methodC :: String -> String 

這顯然輸出正確的錯誤:The type signature for ‘methodB’ lacks an accompanying binding。 例如,我想要類似於下面的內容來工作。

GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help 
Prelude> :l example.hs 
[1 of 1] Compiling X    (example.hs, interpreted) 

example.hs:6:1: error: 
    The type signature for ‘methodB’ lacks an accompanying binding 

example.hs:8:1: error: 
    The type signature for ‘methodC’ lacks an accompanying binding 
Failed, modules loaded: none. 
Prelude> methodA "jamesmstone" 

<interactive>:2:1: error: 
    Variable not in scope: methodA :: [Char] -> t 

回答

5

您可以使用-fdefer-type-errors。這會給你關於類型錯誤的警告,但不會阻止你運行你的程序的其他類型良好的部分。

例子:

如果Program.hs包含:

foo :: Int 
foo = 'a' 

main = putStrLn "Hello, world" 
與-fdefer型錯誤

,那麼你仍然可以加載和運行main

$ ghci -fdefer-type-errors Program.hs 
GHCi, version 7.10.2: http://www.haskell.org/ghc/ :? for help 
[1 of 1] Compiling Main    (Program.hs, interpreted) 

Program.hs:5:7: Warning: 
    Couldn't match expected type ‘Int’ with actual type ‘Char’ 
    In the expression: 'a' 
    In an equation for ‘foo’: foo = 'a' 
Ok, modules loaded: Main. 
*Main> main 
Hello, world 
*Main> 
3

只要給他們一個綁定不起作用:

methodB = undefined 
methodC = undefined 
相關問題