可变参数Ada函数 [英] Variadic Ada Functions

查看:89
本文介绍了可变参数Ada函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究Ada,因为我对严格的类型安全性和编程合同的想法很感兴趣。 永远编程的想法很好。无论如何,真正的问题是Ada是否具有可变参数功能。在SO 建议上搜索Ada没有的建议,而正确的方法是使用长度不受限制的无约束数组在运行时确定。

I'm studying Ada because I am intrigued by the idea of strict type safety and programming contracts. The idea of "programming for forever" is nice. Anyway, the real question is whether or not Ada has variadic functions. A search on SO suggests that Ada doesn't, and the correct way to do this is with an unconstrained array whose length is determined at runtime.

我的问题是,您不是怎么做的,而是正确地执行此操作的惯例是什么?

My question then, isn't how do you do it, but rather what is the convention for doing it correctly?

另外,为什么Ada可以执行+(例如1 + 2 + 3)之类的(似乎是基于堆栈的)操作,但对函数调用的参数却不能执行相同的操作?

Additionally, why is it that Ada can perform (what appear to be stack-based) operations like + (e.g. 1+2+3), but it cannot do the same for arguments to a function call?

不像
Max(1、2、3,...,n)那样完全不执行可变参数表达式是否更习惯? code>,或者仅仅是将参数传递给它,例如 Args.len = n;。 Max(Args [])

我的直觉和我从阅读各种Ada书籍中得到的启示表明,您不应该拥有

My instinct and what I've gleaned from reading the various Ada books suggests that you shouldn't have unspecific functions due to them being less safe.

推荐答案

如建议的此处,杂色函数可能会暴露某些语言中的类型安全性问题。尽管Ada不支持可变参数功能,但在此处看到的示例使用 操作员重载 = http://www.ada-auth.org/standards/12rm/html/RM-4-3-3.html rel = nofollow> 数组聚合 。可变参数列表构成的风险通常在于允许将原始输入数据直接传递到可执行代码。插入的汇总排除了Ada中的这种情况,同时允许了样式上的方便。

As suggested here, "variadic functions can expose type-safety problems in some languages." Although Ada does not support variadic functions, the example seen here mimics the behavior using operator overloading in an array aggregate. The risk posed by a variadic parameter list typically lies in allowing raw input data to be passed directly to executable code. The interposed aggregate precludes this in Ada, while permitting the stylistic convenience.

在下面的片段中,重载的 + 函数将每个字符串文字转换为 Unbounded_String ,每个这样的 Unbounded_String 成为的组成部分Variadic_Array ,它随后成为 Print_Line 函数的唯一参数。如果 Constraint_Error .html rel = nofollow>规定的检查失败。

In the fragment below, an overloaded + function converts each string literal into an Unbounded_String, and each such Unbounded_String becomes a component of a Variadic_Array, which then becomes the sole parameter to the Print_Line function. Constraint_Error is raised if any of the prescribed checks fail.

Print_Line((+"Mary", +"had", +"a", +"little", +"lamb."));

这篇关于可变参数Ada函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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