我已经开始检查项目中的一些代码,发现这样的事情:
GC.Collect();
GC.WaitForPendingFinalizers();
Run Code Online (Sandbox Code Playgroud)
这些线通常出现在被设想在提高效率的基础上破坏对象的方法上.我发表了这样的言论:
1,2和3都是真的吗?你能给出一些支持你答案的参考吗?
虽然我几乎可以肯定我的言论,但我需要明确我的论点,以便向我的团队解释为什么这是一个问题.这就是我要求确认和参考的原因.
我最近尝试将我的个人博客网站部署到我的远程服务器。当我尝试通过执行将一些文件和目录移动到另一个地方时mv,发生了一些意外错误。命令行回显“目录不为空”。在进行了一些谷歌搜索后,我再次尝试使用 ' -f ' 开关或 ' -v ',显示相同的结果。我是用root账号登录的,流程在这里:
root@danielpan:~# shopt -s dotglob
root@danielpan:~# mv /var/www/html/wordpress/* /var/www/html
mv: cannot move `/var/www/html/wordpress/wp-content` to `/var/www/html/wp-content`:
Directory not empty
root@danielpan:~# mv -f /var/www/html/wordpress/* /var/www/html
mv: cannot move `/var/www/html/wordpress/wp-content` to `/var/www/html/wp-content`:
Directory not empty
Run Code Online (Sandbox Code Playgroud)
有人知道为什么吗?
(我正在运行 Ubuntu 14.04)
我有一个需要协变,因此,在另一个类*的覆盖可以返回一个类型的住房Shelter<Cat>,其中一个Shelter<Animal>预期.由于类在C#中不能是共变或逆变,我添加了一个接口:
public interface IShelter<out AnimalType>
{
AnimalType Contents { get; }
}
Run Code Online (Sandbox Code Playgroud)
然而,有一个地方,IShelter(编译时类型)被分配了一个新的动物,我们确定所设置的动物将成为一只猫.起初,我以为我可以在Contents属性中添加一个集合并执行:
IShelter<Cat> shelter = new Shelter(new Cat());
shelter.Contents = new Cat();
Run Code Online (Sandbox Code Playgroud)
但是添加setter是不可能的;
Error CS1961 Invalid variance: The type parameter 'AnimalType' must be invariantly valid on 'IShelter<AnimalType>.Contents'. 'AnimalType' is covariant.
Run Code Online (Sandbox Code Playgroud)
这是有道理的,因为否则我可以将cathelter传递给这个函数:
private static void UseShelter(IShelter<Animal> s)
{
s.Contents = new Lion();
}
Run Code Online (Sandbox Code Playgroud)
但是,我不打算这样做.有一些方法可以将setter标记为不变量,这样UseShelter函数只能分配一个Animal,这样就可以在编译时强制执行.我需要这个的原因是因为我的代码中有一个地方知道它有一个Shelter<Cat>,并且需要将Contents属性重新分配给一个新的Cat.
到目前为止我找到的解决方法是在显式set函数中添加一个jucky运行时类型检查; Juck!
public void SetContents(object newContents)
{
if (newContents.GetType() != typeof(AnimalType))
{
throw new InvalidOperationException("SetContents must be given the correct …Run Code Online (Sandbox Code Playgroud) 我正在阅读 Barnes 的书“Programming in Ada 2012”。这是实现第 12.5 节中的堆栈的代码示例。
src/stacks.adb:(主要相关文件)
package body Stacks is
procedure Push(S: in out Stack; X: in Integer) is
begin
S := new Cell'(S,X);
end Push;
procedure Pop(S: in out Stack; X: in out Integer) is
begin
X := S.Value;
S := Stack(S.Next);
end Pop;
function "="(S, T: Stack) return Boolean is
SS: access Cell := S;
TT: access Cell := T;
begin
while SS /= null and TT /= null loop
if SS.Value /= TT.Value then …Run Code Online (Sandbox Code Playgroud) Gnat 测试的文档展示了如何生成线束和框架单元测试。它为目标项目中的每个公共函数创建一个单元测试,但如果我添加另一个单元测试,那么当我重新生成线束和骨架单元测试时,该单元测试就会被覆盖。
在gnattest 提供的简单示例项目中,如何添加另一个测试?我试过了:
(1) 在已有的测试函数中添加另一个 Assert 行。这可行,但不是好的做法;第一个失败的测试会阻止其他测试的运行。
(2)向obj/gnattest/harness/gnattest.xml添加函数定义然后重新生成测试不起作用;xml 文件在用于创建测试存根之前会重新生成。
(3) 通过以下更改手动将定义添加到测试/和利用/,这些更改会因重新生成测试而受到破坏。
将其添加到 obj/gnattest/tests/simple-test_data-tests.ads:
procedure Test_Inc2_4f8b9f (Gnattest_T : in out Test);
Run Code Online (Sandbox Code Playgroud)
将其添加到 obj/gnattest/tests/simple-test_data-tests.adb:
-- begin read only
procedure Test_Inc2 (Gnattest_T : in out Test);
procedure Test_Inc2_4f8b9f (Gnattest_T : in out Test) renames Test_Inc2;
-- id:2.2/4f8b9f38b0ce8c74/Inc/1/0/
procedure Test_Inc2 (Gnattest_T : in out Test) is
-- simple.ads:7:4:Inc
-- end read only
begin
Assert (Inc(2) = 2, "this test should fail");
-- begin read only
end Test_Inc2;
-- …Run Code Online (Sandbox Code Playgroud) 在Ada中,上下文可以确定“ +”不是字符串,而是整数运算符,如表达式:所示"+"(5,2)。问题是,如何将该运算符存储在变量中?我想将该整数运算符或其他一些运算符作为接受两个Integer并返回Integer的二进制函数传递。在下面的代码中,我做了一个明确的函数,它仅调用运算符,可以将其用作解决方法。有什么方法可以避免使用该包装器,而直接传递(访问)Integer的“ +”运算符?
with Ada.Text_IO; use Ada.Text_IO;
procedure operator is
type binary_int_operator is access function(lhs : Integer; rhs : Integer) return Integer;
--plus : binary_int_operator := Integer."+"'Access;
--plus : binary_int_operator := Integer'Access("+");
--plus : binary_int_operator := Integer'"+";
--plus : binary_int_operator := "+";
function plus(lhs : Integer; rhs : Integer) return Integer is
begin
return lhs + rhs;
end plus;
begin
Put_Line(Integer'Image("+"(5, 12)));
end operator;
Run Code Online (Sandbox Code Playgroud)
带注释的声明显示了我所做的一些尝试,但未编译。
此(声明)代码引发存储错误:
type Vector is array (Integer range <>) of Integer;
type Array_Starting_At_One (Max : Integer := 0) is
record
Mat : Vector (1 .. Max);
end record;
X : Array_Starting_At_One;
Run Code Online (Sandbox Code Playgroud)
我不明白为什么。如果我指定默认值,X : Array_Starting_At_One (Max => 0);则错误消失,尽管 Array_Starting_At_One 类型规范仍会触发警告,即创建此类对象可能会引发 Storage_Error。
我什至不想存储一个位,所以这个错误对我没有任何意义:
raised STORAGE_ERROR : System.Memory.Alloc: object too large
Run Code Online (Sandbox Code Playgroud) 我有一个项目,我经常在其中的某个目录中创建许多新的主入口点 *.adb 文件。有什么方法可以使用 gprbuild 设置我的项目,以便添加新的主程序不需要编辑 .gpr 项目文件?
目前,我有这个,每次添加新电源时都需要修改电源列表:
project Adabots is
for Source_Dirs use ("src", "src/examples");
for Main use ("build_wall.adb", "remove_wall.adb", "get_stone.adb", "staircase_down.adb", "josephine.adb", "dig_cavern.adb", "build_maze.adb", "elevator.adb", "lovelace.adb", "dig_hallway.adb", "spiral_staircase.adb", "walk_up_stairs.adb");
Run Code Online (Sandbox Code Playgroud)
但是我想要做的只是说里面的每个 .adb 文件src/examples都应该被视为主文件。
这些信息可以在其他地方找到,但没有合并到这个特定的用例中,因此我觉得需要一个我(和其他人)可以参考的 stackoverflow 自我回答。如果我遗漏了什么,请随时添加您自己的答案。
project some_project is
for Source_Dirs use ("src");
for Object_Dir use "obj";
for Main use ("main.adb");
package Compiler is
for Default_Switches ("ada") use ("-O1", "-gnatwae");
end Compiler;
end some_project;
Run Code Online (Sandbox Code Playgroud)
我想让这个生成../some_program_name.exe. 如何指定该 gpr 项目将生成的可执行文件的名称?可以在上面的目录中吗?
我有一个 gpr 项目,它使用 gnatprep 来预处理源文件。但现在我有一个工具需要已经预处理的源文件。我知道我可以找到每个源文件并通过 gnatprep 运行它:
find . -type f -iname '*.ad[sb]' -exec gnatprep -DSymbol=value {} {}.prep \;
Run Code Online (Sandbox Code Playgroud)
但我想利用项目文件来找到正确的源文件并传递它们。我的项目文件还定义了要使用的各种符号值,我必须将其添加到上面的命令中。是否可以通过 .gpr 文件中的某些参数?例如
for Object_Dir use 'obj';
for Preprocessed_Sources_Dir use 'wow_that_was_easy';
Run Code Online (Sandbox Code Playgroud)