Haskell:GHCi尽可能地加载,而不是失败或错误

jam*_*one 2 haskell ghci

在使用GHCi的Haskell中,有一种方法可以加载下面的文件,它允许您测试具有绑定的方法.

使用案例:尝试测试我的模块的一部分,同时具有其余部分的框架代码.(至于没有XY问题)

module X (methodA, methodB, methodC) where

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

methodB :: Int -> String

methodC :: String -> String
Run Code Online (Sandbox Code Playgroud)

这显然会输出正确的错误: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
Run Code Online (Sandbox Code Playgroud)

Eri*_*ikR 5

你可以用-fdefer-type-errors.这将为您提供有关类型错误的警告,但不会阻止您运行程序中其他类型良好的部分.

例:

如果Program.hs包含:

foo :: Int
foo = 'a'

main = putStrLn "Hello, world"
Run Code Online (Sandbox Code Playgroud)

然后使用-fdefer-type-errors,您仍然可以加载并运行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> 
Run Code Online (Sandbox Code Playgroud)