Hoe kan ek aantoon dat 'n metode nog nooit 'n nul behulp kode kontrakte terug?
-
06-09-2019 - |
Vra
Hoe kan ek aantoon dat 'n metode nog nooit 'n nul terug? Tans is hierdie my kode.
Line 19 kry 'n Verseker nie bewys boodskap, selfs al CreateFunction aanvaar dat die resultaat is nie niks nie.
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
Oplossing
Is
Contract.Ensures(Contract.Result() != null);
werk? Basies, sou ek probeer af paring dit totdat jy die eenvoudigste geval wat nie werk as jy sou verwag en gaan van daar af te kry.
- MarkusQ
Ander wenke
Jy moet die Assume
in CreateFunction
verander na 'n Ensures
. Daarna moet jy okay wees. Onthou, Assume
is vir interne aannames ten einde die statiese checker plaaslik help. Hulle is nie sigbaar van ander metodes. Slegs Requires
en Ensures
is kruis-metode kontrakte.
Gelisensieer onder: CC-BY-SA met toeskrywing
Nie verbonde aan StackOverflow