小编Ced*_*Sun的帖子

自动引入do ... end块对goto语句如何起作用?

LUA文档的评论对标签范围:

goto可以跳到任何可见的标签,只要它不进入局部变量的范围内

因此,据我了解,以下代码是有问题的:

-- codelist 1
goto a
local x = 42
::a::
Run Code Online (Sandbox Code Playgroud)

但是它在lua web shell中效果很好。该文档继续说:

请注意,您可以想到

do
  <...>
  --::a::
  goto a  -- invalid (forward jump into scope of local definition)
  goto b  -- valid (jump out of block)
  <...>
  local x
  <...>
  ::a::
  <...>
  --goto a
  ::b::
end
Run Code Online (Sandbox Code Playgroud)

相当于

do
  <...>
  --::a::
  goto a  -- invalid (jump into nested block prohibited because nested label not even visible here)
  goto b  -- valid (jump out of …
Run Code Online (Sandbox Code Playgroud)

lua

6
推荐指数
1
解决办法
68
查看次数

“viewable_range”概念有什么意义?

[范围.细化]

\n
\n

viewable_\xc2\xadrange 概念指定了可以安全转换为视图的范围类型的要求。

\n
\n

它的强制实现粗略地表明,如果满足以下任一条件,则range进一步满足viewable_range

\n
    \n
  1. 它只是一个视图,例如std::string_view,或
  2. \n
  3. 它是一个左值引用(即使它的引用删除类型不是视图),例如std::vector<int>&,或
  4. \n
  5. 它是可移动对象类型(即不是引用类型),例如std::vector<int>
  6. \n
\n

我的问题是:

\n
    \n
  1. 这个概念体现了什么想法?具体来说,它的实例如何“安全地转换为视图”以及为什么我想要这样的转换?这里的“安全”到底意味着什么?
  2. \n
  3. 您总是使用viewable_range约束通用参考吗?(即被推导为左值引用类型的唯一机会T。)这是标准范围适配器(闭包)对象的情况。
  4. \n
  5. range在 C++23 中,范围适配器(闭包)对象被修改为采用 a作为第一个参数。viewable_range从那时起概念还有其他用途吗?
  6. \n
  7. 对于应用程序开发,什么时候使用orviewable_range代替?viewrange
  8. \n
\n

c++ c++-concepts range-v3 c++20

5
推荐指数
1
解决办法
429
查看次数

为什么抛出upcasting检查异常

我注意到一些复杂库中的方法,包括标准JDK,倾向于throws向上转换异常.起初我在Apache POI的源代码中发现了这种现象,稍后会再次看到它java.io.FileWriter:

public FileWriter(String fileName) throws IOException {
    super(new FileOutputStream(fileName));
}
Run Code Online (Sandbox Code Playgroud)

其中实例化FileOutputStream声明FileNotFoundException此callstack中唯一的已检查异常.然而,IOException声明,这是超级类FileNotFoundException.

那是什么原因呢?还是仅仅依赖于程序员的习惯?

java exception-handling upcasting checked-exceptions

2
推荐指数
1
解决办法
121
查看次数

重构方法使用泛型

起初我写道:

private ArrayList<Integer> getDataList() {
    ArrayList<Integer> dataList = new ArrayList<>(LEN);
    for (int i = 0; i < LEN; i++)
        dataList.add(i);
    Collections.shuffle(dataList);
    return dataList;
}
Run Code Online (Sandbox Code Playgroud)

后来我决定使用泛型:

private <E> ArrayList<E> getDataList() {
    ArrayList<E> dataList = new ArrayList<>(LEN);
    for (int i = 0; i < LEN; i++)
        dataList.add(/* a procedure that generates E instance from index i*/);
    Collections.shuffle(dataList);
    return dataList;
}
Run Code Online (Sandbox Code Playgroud)

接口中的静态方法不能覆盖,因此无法在E上调用静态方法来生成实例.

如何重构这个使用泛型?谢谢.

java generics

2
推荐指数
1
解决办法
66
查看次数