简体   繁体   English

结果附加或异常

[英]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_”前缀是什么?

  1. The first example doesn't have the as ____ clutter, which makes it easier to read and comprehend (IMHO).第一个示例没有as ____的混乱,这使它更容易阅读和理解(恕我直言)。
  2. The "al_" prefix is a naming convention I follow, where "al" means "attachment local". “al_”前缀是我遵循的命名约定,其中“al”表示“附件本地”。 As a reader of my code, I can then look at the variable name and know where the variable was declared.作为我的代码的读者,我可以查看变量名并知道变量的声明位置。
  • 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_”,那么我会查看checkif.. 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.

相关问题 埃菲尔附加声明中的局部变量有什么意义? - What's the point of the local variable in Eiffel's attached-statement? 必须通过强制检查将附加的本地声明为可分离属性的语义 - semantic of having to declare an attached local into detachable attribute with mandatory check 埃菲尔铁塔:古典式压铸开关结构,附带并检查 - Eiffel: classical typecasting switch structure with attached and inspect 埃菲尔:有没有一种方法可以在没有任何附加实例的情况下测试类的给定泛型参数? - Eiffel: Is there a way to test a given Generic parameter of a Class without any attached instance of it? 埃菲尔铁塔创建对象和函数结果的最短语句 - Eiffel shortest statement for object creation and result of function 救援如何进一步提出或忘记异常 - rescue how to raise further or forget an exception 埃菲尔铁塔(Eiffel):附加到check语句中的未知标识符 - Eiffel: Unknown identifier on attached check into require statement 在Eiffel网络上启动客户端 - 服务器模型的开发人员例外 - Developer exception starting a client-server model on Eiffel net Eiffel:类型转换运算符〜/和附加语句之间有什么区别? - Eiffel: type casting operators whats the difference between ~ / and attached statements? 埃菲尔例外不起作用 - Eiffel exception not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM