【问题标题】:Ada Modeless Subprogram ParametersAda 无模式子程序参数
【发布时间】:2013-07-09 14:13:48
【问题描述】:

如果 Ada 中的参数保持无模式,会发生什么?

有什么区别

procedure my_func ( param_1 : in param_type )

和

procedure my_func ( param_1 : param_type )

我是 ada 的新手,我的大部分程序都是后者编写的。程序按预期编译并运行。

【问题讨论】:

    标签: ada


    【解决方案1】:

    没有区别——如果没有给出参数模式,编译器假定“in”。

    见http://www.ada-auth.org/standards/12rm/html/RM-6-1.html 18/3 开始的行。

    -- 马丁

    【讨论】:

    • 以前所有函数参数都是in,所以很多人觉得说in是没有意义的。我发现我仍然这样做!
    【解决方案2】:

    按照 Martin 的建议,如果未提供,则默认模式为“in”。

    我想补充一点,如果可能的话,您有时可以尝试一些您怀疑的事情。 就像看下面的简单代码一样,我没有为争论“no_1”提供任何模式。 正如所见,我正在为其分配 'no_2' 的值。

    with Ada.Text_IO; use Ada.Text_IO;
    
    procedure just_testing is
    
        procedure get_value (no_1 : Integer);
    
        procedure get_value (no_1 : Integer) is
            no_2 : Integer := 2;
        begin
            no_1 := no_2;
        end get_value;
    
    begin
    
        Put("auto mode");
    
    end just_testing;
    

    当我编译这段代码时,看看我们得到了什么错误。

    >gnatmake just_testing.adb
    gcc -c just_testing.adb
    just_testing.adb:10:09: assignment to "in" mode parameter not allowed
    gnatmake: "just_testing.adb" compilation error
    

    因此,编译器明确表示默认模式为“in”,因为我们无法为模式为 in 的参数分配任何值。

    【讨论】:

      猜你喜欢
      • 2021-12-31
      • 2021-05-31
      • 2022-10-18
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 2013-04-17
      • 1970-01-01
      相关资源
      最近更新 更多