[英]Result attached or exception
Let's say that I have a function f
which should return an attached T
by calling g
.假设我有一个 function
f
应该通过调用g
返回attached T
。 However, g
returns a detachable T
.但是,
g
返回一个detachable T
。 If g
results in a Void, I want to raise an exception like this:如果
g
导致 Void,我想引发这样的异常:
f: T
do
if attached g as res then
Result := res
else
raise
end
end
raise
do
(create {DEVELOPER_EXCEPTION}).raise
end
In this setup EiffelStudio gives me an error VEVI: Variable is not properly set. Variable: Result
在此设置中,EiffelStudio 给了我一个错误
VEVI: Variable is not properly set. Variable: Result
VEVI: Variable is not properly set. Variable: Result
at the end of f
. VEVI: Variable is not properly set. Variable: Result
f
末尾的结果。
Indeed, Result can be Void at the end of f
but the execution should not reach the end of f
in this case, an exception should have been raised.实际上, Result 可以在
f
的末尾为 Void 但在这种情况下执行不应到达f
的末尾,应该引发异常。
How can I restructure the code to achieve a similar result?如何重构代码以获得类似的结果?
If the type of a raised exception does not matter, the following code will do:如果引发异常的类型无关紧要,则可以使用以下代码:
f: T
do
Result := g
check is_g_attached: attached Result then end
end
If the type of the raised exception is important, the feature raise
can be augmented with the postcondition False
that indicates that the feature never returns.如果引发异常的类型很重要,则可以使用表示该特性永远不会返回的后置条件
False
来增加特性raise
。 Then, the code would look like然后,代码看起来像
f: T
do
Result := g
if not attached Result then
raise
end
end
raise
do
(create {DEVELOPER_EXCEPTION}).raise
ensure
False
end
Just found out that check
s can be used in this case:刚刚发现在这种情况下可以使用
check
:
f: T
do
if attached g as res then
Result := res
else
raise
end
check attached Result then end
end
However, I wonder if there is a cleaner way.但是,我想知道是否有更清洁的方法。
I think you can get away with:我认为你可以逃脱:
f:T
do
check has_g: attached g then Result := g end
end
The code will naturally break if the check-condition attached g
is not True
.如果
attached g
不是True
,代码自然会中断。 Note that I use this construct quite a bit when I want to ensure that a feature like g
is attached.请注意,当我想确保附加诸如
g
之类的功能时,我会使用此构造。
I will also code it as:我还将其编码为:
f:T
do
check has_g: attached g as al_result then Result := al_result end
end
So, you might ask: How is this different and what is the "al_" prefix about?所以,你可能会问:这有什么不同,“al_”前缀是什么?
as ____
clutter, which makes it easier to read and comprehend (IMHO).as ____
的混乱,这使它更容易阅读和理解(恕我直言)。 If the variable has no prefix, then it is a feature reference on the class itself.如果变量没有前缀,则它是 class 本身的特征参考。
If the prefix is "l_?", then I look to the feature local
declarations.如果前缀是“l_?”,那么我会查看功能
local
声明。
If "al_", then I look to a check
or if.. then
or other block for an as
keyword.如果是“al_”,那么我会查看
check
或if.. then
或其他块as
关键字。
I also use this for across loops, where ic_?
我也将它用于跨循环,其中
ic_?
is an "Iteration Cursor" local object.是一个“迭代游标”本地 object。 I use this naming convention applied to symbolic loops as well.
我也将此命名约定应用于符号循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.