什么是一个语句的非到达终点(可达端点)? [英] What is a non-reachable end point(unreachable endpoint) of a statement?

查看:131
本文介绍了什么是一个语句的非到达终点(可达端点)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从这个答题

在C#中的下一个非空单?

这个术语的发生难度出现在C#语言规范

The only occurence of this terminology is appeared in §6.5 of C# Language Specification

    $ b的§6.5 $ b
  • 如果 D 具有非void返回类型和 F中的身体是一个语句块,当˚F每个参数都给出了相应参数的类型在 D ,身体˚F是一个有效的语句块(WRT第8.2节),以不可达的终点,其中每个return语句指定是一个表达式隐式转换为 D 的返回类型。

  • If D has a non-void return type and the body of F is a statement block, when each parameter of F is given the type of the corresponding parameter in D, the body of F is a valid statement block (wrt §8.2) with a non-reachable end point in which each return statement specifies an expression that is implicitly convertible to the return type of D.

后者的规范,我们可以看到

Latter in the specification we can see


  • 8.1结束点和可到达


    每一个语句具有的终点的。直观地讲,语句的结束点是紧跟在语句中的位置。复合语句的执行规则(包含嵌入的陈述),规定了当控制到达一个嵌入语句的结束点时所采取的动作。例如,当控制到达在一个块一个语句的结束点时,控制被转移到在该块中的下一个语句。

    ...

  • 8.1 End points and reachability

    Every statement has an end point. In intuitive terms, the end point of a statement is the location that immediately follows the statement. The execution rules for composite statements (statements that contain embedded statements) specify the action that is taken when control reaches the end point of an embedded statement. For example, when control reaches the end point of a statement in a block, control is transferred to the next statement in the block.
    ...

和我们可能有一些感觉。但是,我用Google搜索,发现有没有直接的解释非可达端点。因为Stack Overflow是一个Q&放大器;一个网站,我会想,如果有一个更简单,更直观的解释,可以很容易地被搜索这个术语的理解将是程序员尤其是谁不以英语为母语的帮助。

And we might have some sense of it. However, I googled and found there's no a directly explanation of non-reachable endpoint. Because Stack Overflow is a Q&A site, I would think if there is a simpler and more intuitive explanation, can easily be searched and understand of this terminology would be helpful for programmers especially who are not native English speakers.

推荐答案

Ben的回答给了它一个很好的感觉。要有点更精确的终点:

Ben's answer gives a good sense of it. To be a bit more precise, the end points of:



  • 继续
  • 转到

  • 收益


  • break
  • continue
  • goto
  • return
  • throw

语句是不可达。这些声明将控制权转移别处之前结束中的每一个,所以该语句的终点永远不会打。与此相比,类似的语句:

statements are not reachable. Each one of those statements transfers control somewhere else "before it ends", so the "end point" of the statement is never hit. Compare that to statements like:


  • Console.WriteLine();

  • 我++;

和等,其中控制转移到下一条语句。

and so on, which transfer control to the next statement.

环路目前的有趣的挑战:

Loops present an interesting challenge:

while(x) { M(); }

这在本质上是一样的:

BEGIN: 
if (!x) goto END;
{ M(); }
goto BEGIN;
END: ;



所以端点可达。但

So the endpoint is reachable. But

while(true) { M(); }



可以优化:

can be optimized to:

BEGIN: 
{ M(); }
goto BEGIN;



由于没有办法到达终点现在,这种说法被认为有无法到达终点。无论这个无限循环,或M()永远不会返回,或M()抛出;无论哪种方式,语句的结束点从未达到过。

Since there is no way to "get to the end" now, this statement is considered to have an unreachable end point. Either this loops forever, or M() never returns, or M() throws; either way, the end point of the statement is never reached.

有关确定可达的确切规则有点比这个草图更为复杂,但给你的感觉他们。 (我想给的人,看看他们是否有可达一抓一个挑战是:写一个有到达的goto语句的程序,但相应的标记语句不可达整蛊!)

The exact rules for determining reachability are a bit more complicated than this sketch, but that gives you a sense of them. (A challenge I like to give people to see if they have a grasp of reachability is: write a program which has a reachable goto statement but the corresponding labelled statement is unreachable. Tricky!)

这些规则在许多地方使用。三是立即浮现在脑海:首先,开关部分必须没有到达终点。其次,一个方法(或lambda或财产吸气等),即非void不能有一个到达终点。第三,如果一个方法具有可到达终点,它有一个out参数,参数必须被明确在终点分配。

These rules are used in a number of places. Three that immediately come to mind: First, a switch section must NOT have a reachable end point. Second, a method (or lambda, or property getter, etc) that is non-void must not have a reachable end point. Third, if a method has a reachable end point and it has an out parameter, the parameter must be definitely assigned at the end point.

这篇关于什么是一个语句的非到达终点(可达端点)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆