gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:52:53 25 4
gpt4 key购买 nike

在使用 GHCi 的 Haskell 中,有一种方法可以加载如下所示的文件,让您可以测试具有绑定(bind)的方法。

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

module X (methodA, methodB, methodC) where

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

methodB :: Int -> String

methodC :: String -> String

这显然会输出正确的错误:“methodB”的类型签名缺少附带的绑定(bind)。例如,我想要类似于下面的东西来工作。

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

最佳答案

您可以使用-fdefer-type-errors。这将向您发出有关类型错误的警告,但不会阻止您运行程序的其他类型正确的部分。

例子:

如果 Program.hs 包含:

foo :: Int
foo = 'a'

main = putStrLn "Hello, world"

然后使用 -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>

关于Haskell:尽可能多地加载 GHCi,而不是失败或错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39136407/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com