任何人都可以给我一个“单引号”的摘要在艾达中的用法?

发布于 2024-12-06 22:59:55 字数 1127 浏览 3 评论 0原文

我刚刚读过“Ada 编程”,但我对如何在 Ada 中使用 ' (单引号)有点困惑。

我可以理解 ' 用于参考属性。 AAA'Image(..), BBB'Value(..)

但是,考虑这段代码:

   type Plain_Vector (Capacity : Capacity_Subtype) is record
      Elements : Elements_Array (1 .. Capacity);
      Last     : Extended_Index := No_Index;
      Busy     : Natural := 0;
      Lock     : Natural := 0;
   end record;
 ------------------------------------------------------------------
   new Plain_Vector'(2, (Left, Right), Last => Last, others => <>)

Q1:“new”语句的参数如何匹配类型的参数和记录字段?

 I can GUESS "2" matched "Capacity", 
             "(Left, Right)" matched "Elements", 
             "Last => Last" matched "Last"
             "Others => <>" matched "Busy" and "Lock" to let them use default value.

但这只是猜测,对此有任何官方语法解释吗?

问题2:'有什么作用?(在“new”语句中)

它是一个属性还是它有其他含义吗?

在哪里我可以找到Ada 中“单引号”的用法摘要吗?

我花了很长时间试图找出这些信息,但没有运气。

先感谢您。迈尔斯。

I've just read "Ada Programming" but I'm a bit confused about how to use ' (single quote mark) in Ada.

I can understand that ' is used for reference attribute. AAA'Image(..), BBB'Value(..)

However, considering this piece of code:

   type Plain_Vector (Capacity : Capacity_Subtype) is record
      Elements : Elements_Array (1 .. Capacity);
      Last     : Extended_Index := No_Index;
      Busy     : Natural := 0;
      Lock     : Natural := 0;
   end record;
 ------------------------------------------------------------------
   new Plain_Vector'(2, (Left, Right), Last => Last, others => <>)

Q1: How the "new" statement's arguments matches the type's parameter and record fields?

 I can GUESS "2" matched "Capacity", 
             "(Left, Right)" matched "Elements", 
             "Last => Last" matched "Last"
             "Others => <>" matched "Busy" and "Lock" to let them use default value.

But this is just a GUESS, are there any official grammar explanation on this?

Q2: What does the ' do? (in the "new" statement)

Is it an attribute or does it have any other meanings?

Where can I find a summary usage of "single quote mark" in Ada?

I spent long time trying to find out those information, but no luck.

Thank you in advance. Miles.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

电影里的梦 2024-12-13 22:59:55

如果您有《Ada 参考手册》的电子版,您可以在语法摘要中搜索 ' 字符(我拥有的最新版本中的附件 P;请检查目录)。

' 字符用于:

  • 字符文字:'x'
  • 属性引用:Foo'Size
  • 限定表达式:Some_Type'(expression ), Some_Type'Aggregate

它也用于表示子句(现在称为“方面子句”);这些看起来很像属性引用:for Foo'Size use 32;

当然,它可以出现在注释、字符串或字符中。

您发布的代码中的示例是限定表达式。

建议:在字符文字以外的上下文中,字符 ' 可能应该被称为撇号,因为它不充当引号。对于属性和限定表达式,有时发音为“tick”:我将 Foo'Size 读作“foo tick size”。

new 是一个表达式,而不是一个语句。)

If you have a soft copy of the Ada Reference Manual, you can search for the ' character in the Syntax Summary (it's Annex P in the latest version I have; check the table of contents).

The ' character is used for:

  • Character literals: 'x'
  • Attribute references: Foo'Size
  • Qualified expressions: Some_Type'(expression), Some_Type'Aggregate

It's also used in representation clauses (now called "aspect clauses"); these look a lot like attribute references: for Foo'Size use 32;.

And of course it can appear in a comment or in a string or character literal.

The example in the code you posted is a qualified expression.

Suggestion: In contexts other than character literals the character ' should probably be referred to as an apostrophe, since it's not acting as a quotation mark. For attributes and qualified expressions, it's sometimes pronounced "tick": I'd read Foo'Size as "foo tick size".

(And new is an expression, not a statement.)

迷雾森÷林ヴ 2024-12-13 22:59:55

您似乎在专门询问限定表达式(基思在回答中的第三个项目符号)。

在 Ada 中,如果您有两个不同类型的对象,您可以尝试使用目标类型名称(如函数名称)在它们之间进行转换,如下所示:

Foo :constant Integer := Integer (2.35);

一般来说,只有当两种类型都是数字类型,或者一种类型派生于另一种类型时(声明为 type New_Type is new Old_Type ...),这才有效。

编译器当然必须添加代码来验证该值是否符合目标类型可能具有的任何约束。但这对于简单的类型转换非常有用。

然而,当您处理表达式时,有时您想要的不是转换,而是告诉编译器将表达式设为什么类型。不需要(运行时)代码来执行此操作,只需将表达式设置为我告诉您的类型即可。

编译器通常可以从上下文中找出这一点,但有时却不能。这就是撇号的用武之地。它告诉编译器不要将表达式转换为指定类型,而是首先将其创建为该类型。

最常见的用途是执行动态分配时,如示例所示。有时可能还有其他情况需要它。一个示例可能是将文字值传递到重载例程中。假设您有两个版本的过程 My_Routine,一个版本接受 Integer,另一个版本接受不同的自定义整数类型。如果将对象传递给它,编译器只能查看对象的类型。但是,如果您传入文字 1,很可能会收到编译器错误,表明表达式不明确。

您可以通过将文字 1 放入常量整数并将其传入(然后抱怨您愚蠢的编译器)来解决此问题。然而,更容易做的事情如下:

My_Routine (Integer'(1));

这解决了编译器的歧义。这不是“转换”,因此不需要额外的代码。您只是告诉编译器以下表达式的类型为Integer

You seem to be asking specifically about qualified expressions (Keith's third bullet in his answer).

In Ada, if you have two objects of different types you can attempt to convert between them by using the destination type's name like a function name, like so:

Foo : constant Integer := Integer (2.35);

Generally this only works if both types are numeric types, or if one is derived from the other (declared as type New_Type is new Old_Type ...).

The compiler will of course have to add code to verify that the value falls within any contraints the destination type may have. But this is very useful for simple type conversions.

However, when you are dealing with expressions, sometimes what you want isn't a conversion, but rather to tell the compiler what type to make the expression. No (runtime) code should be required to do this, just make the expression the type I tell you to.

Compilers can generally figure this out from context, but sometimes they can't. This is where that apostrophe comes in. It tells the compiler not to convert the expression to the specified type, but rather to create it as that type in the first place.

The most common use for this is when performing dynamic allocations, as shown in your example. Sometimes there may be other situations where it is needed though. One example might be when passing a literal value into an overloaded routine. Say you have two versions of the procedure My_Routine, one that takes in an Integer, and the other taking in a different custom integer type. If you pass objects into it, the compiler can just look at the object's type. However, if you pass in a literal 1, most likely you will get a compiler error that the expression is ambiguous.

You could solve this by putting your literal 1 into a constant integer and passing that in (then grumbling about your stupid compiiler). However, the easier thing to do is the following:

My_Routine (Integer'(1));

That resolves the ambiguity for your compiler. This isn't a "conversion", so no extra code is needed. You are just telling the compiler that the following expression is of type Integer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文