如何使用代码契约指示方法永远不会返回 null?

发布于 2024-07-14 16:38:28 字数 1213 浏览 6 评论 0原文

如何指示一个方法永远不会返回 null? 目前这是我的代码。

第 19 行收到 Ensures notproved 消息,即使 CreateFunction 假设结果不是空的。

1         <Pure()> Public Function CreateFunction(Of TArg1, TArg2, TResult)(ByVal body As Func(Of Expression, Expression, BinaryExpression)) As Func(Of TArg1, TArg2, TResult)   
2             Contract.RequiresAlways(body IsNot Nothing)   
3             Contract.Assume(Contract.Result(Of Func(Of TArg1, TArg2, TResult))() IsNot Nothing)   
4   
5             Dim arg1 = Expression.Parameter(GetType(Integer), "arg1")   
6             Dim arg2 = Expression.Parameter(GetType(Integer), "arg2")   
7   
8   
9             Dim temp = Expression.Lambda(body(arg1, arg2), arg1, arg2)   
10             Contract.Assume(temp IsNot Nothing)   
11             Return DirectCast(temp.Compile, Global.System.Func(Of TArg1, TArg2, TResult))   
12         End Function  
13   
14         <Pure()> Public Function Add() As Func(Of T, T, T)   
15             Contract.Ensures(Contract.Result(Of Func(Of T, T, T))() IsNot Nothing)   
16   
17             Dim temp = CreateFunction(Of T, T, T)(AddressOf Expression.AddChecked)   
18             Return temp   
19         End Function  

How do I indicate that a method never returns a null? Currently this is my code.

Line 19 gets an Ensures not proven message, even though CreateFunction assumes that the result is not nothing.

1         <Pure()> Public Function CreateFunction(Of TArg1, TArg2, TResult)(ByVal body As Func(Of Expression, Expression, BinaryExpression)) As Func(Of TArg1, TArg2, TResult)   
2             Contract.RequiresAlways(body IsNot Nothing)   
3             Contract.Assume(Contract.Result(Of Func(Of TArg1, TArg2, TResult))() IsNot Nothing)   
4   
5             Dim arg1 = Expression.Parameter(GetType(Integer), "arg1")   
6             Dim arg2 = Expression.Parameter(GetType(Integer), "arg2")   
7   
8   
9             Dim temp = Expression.Lambda(body(arg1, arg2), arg1, arg2)   
10             Contract.Assume(temp IsNot Nothing)   
11             Return DirectCast(temp.Compile, Global.System.Func(Of TArg1, TArg2, TResult))   
12         End Function  
13   
14         <Pure()> Public Function Add() As Func(Of T, T, T)   
15             Contract.Ensures(Contract.Result(Of Func(Of T, T, T))() IsNot Nothing)   
16   
17             Dim temp = CreateFunction(Of T, T, T)(AddressOf Expression.AddChecked)   
18             Return temp   
19         End Function  

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蹲墙角沉默 2024-07-21 16:38:28

有效吗

Contract.Ensures(Contract.Result() != null);

? 基本上,我会尝试将其削减,直到找到最简单的情况,但不能按您的预期工作,然后从那里开始。

——马库斯Q

Does

Contract.Ensures(Contract.Result() != null);

work? Basically, I'd try paring it down till you find the simplest case that doesn't work as you'd expect and go from there.

-- MarkusQ

快乐很简单 2024-07-21 16:38:28

您需要将 CreateFunction 中的 Assume 更改为 Ensures。 之后你应该没事了。 请记住,Assume 用于内部假设,以便在本地帮助静态检查器。 从其他方法中看不到它们。 只有 RequiresEnsures 是跨方法合约。

You need to change the Assume in CreateFunction to an Ensures. After that you should be okay. Remember, Assume is for internal assumptions in order to help the static checker locally. They are not visible from other methods. Only Requires and Ensures are cross-method contracts.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文