我在函数参数的类型中放置了一个约束,而不是在函数的类型中。
我认为这会导致语法错误或向函数类型添加更多信息。
但看起来约束被完全忽略了。
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE RankNTypes #-}
test :: a -> String
test (n :: (Num a, Ord a) => a) =
if n > 10 then "Hello"
else "World"
main = print "Hello World"
Run Code Online (Sandbox Code Playgroud)
这给出了以下类型错误:
Test3.hs:6:8: error:
• No instance for (Num a) arising from a use of ‘n’
Possible fix:
add (Num a) to the context of
the type signature for:
test :: forall a. a -> String
• In the first argument of …
Run Code Online (Sandbox Code Playgroud) 我试图使用笑话在打字稿中编写单元测试。
// This is how foo() is defined:
// function foo():
// {status: "OK"} |
{status: "ERROR", reason: "INVALID_ID"|"SOME_OTHER_ERROR"};
let res = foo();
expect(res.status).toEqual("ERROR");
expect(res.reason).toEqual("INVALID_ID");
// ^^^ this line gives error TS2339: Property 'reason' does not exist on type ....
Run Code Online (Sandbox Code Playgroud)
打字稿是否具有某种构造,assert(res.status == "ERROR")
例如编译器可以在其中找出结果是这里的第二个变体?
如果没有,是否有其他单元测试框架在其expect()
类似功能中向编译器提供必要的提示?
或者有更好的方法来返回错误foo()
吗?
为一个noobish问题道歉.
我正在开发一个JavaFX应用程序.
我想成立一个"全球性"的布尔类型变量"DebugMode",这样,当一个自定义的(我的意思是定制的,或采用自制)异常被抛出,的getMessage()函数可以检查DebugMode变量,看是否DebugMode是组.
如果DebugMode为'true',则异常可以给出调试的详细消息,或者只是为最终用户提供用户友好消息.
是否有可能在java中,就像PHP中的$ GLOBAL变量一样?或者有没有其他方法为开发人员和最终用户提供两种不同的异常消息?