在Ada中定义通用标量类型的包 [英] Defining a generic scalar-type package in Ada

查看:87
本文介绍了在Ada中定义通用标量类型的包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想通过编写用于处理多项式的软件包来测试编写Ada软件包的过程。可以为多种代数结构定义多项式,因此为了反映这一点,我想使该包通用,以便它可以与Floats,Integers或其他数字子类型一起使用。

I'd like to test the waters of writing Ada packages by making one for manipulating polynomials. Polynomials can be defined for a wide class of algebraic structures, so to reflect this, I'd like to make the package generic, so it can be used with Floats, Integers, or other numeric sub-types.

我现在想说的是,我对Ada的类型系统如何工作或其包装系统如何工作知之甚少。 Web上似乎缺少良好的初学者Ada信息,所以我不得不从这篇对新手不友好的Wikibooks文章。

I want to say now that I know very little about how Ada's type system work or how its package system works. There seems to be a lack of good beginner Ada inforamtion on the web, so I'm having to glean what wisdom I can from this not-so-newbie-friendly Wikibooks article.

页包含有关类型层次结构的一些信息。基于此,看来我的多项式程序包所基于的合理类型应该是 Scalar 类型,因为显然这是定义算术运算的类型。这就是我在 polynomials.ads 中尝试过的事情:

This page has some information about the type hierarchy. Based on that, it would seem that a reasonable type for my Polynomial package to be based on would be the Scalar type, since apparantly that's the one on which arithmetic operations are defined. So this is what I've attempted, in polynomials.ads:

generic

    MAX_DEGREE : Positive;
    type Element is new Scalar;

package Polynomial is

    type Polynomial is Array (0 .. MAX_DEGREE) of Element;

end Polynomial;

但是,这只是让我知道标量未定义来自GNAT的错误。

However, this just nets me a "Scalar" is undefined error from GNAT.

到目前为止,我真的一直在半盲状态,我实际上不知道这些东西是怎么回事作品。如果我似乎有任何重大误解,认为您需要解决,请告诉我。可能最简单的方法是提供我可以从中学习的示例 polynomial.ads polynomial.adb 代码的示例-就像定义了多项式类型(具有通用的最大次数和元素类型)和一个简单的示例函数(如添加两个多项式),因此我可以看到通用函数的工作原理。

So far I've really just been feeling my way around half-blind, I don't actually know how any of this stuff works. If I seem to have any major misconceptions that you think need to be cleared up, please tell me. Probably the easiest would be to provide example polynomial.ads and polynomial.adb code that I can learn from - just like a definition of the Polynomial type (with generic max-degree and element type) and a simple example function like adding two polynomials, so I can see how generic functions work.

PS:有点相关,是否有一种方法可以为用户定义的类型定义属性?

PS: Sort of related, is there a way to define attributes for your user-defined types?

推荐答案

问题仅仅是标量不是类型的名称。

The problem is just that "Scalar" is not the name of a type.

看那篇文章中的泛型形式类型一节,我看不到一个对您施加确切限制的类型想要:任何标量类型。
可惜...
除非有人有更好的主意,否则我将声明扩展为:

Looking at the section "Generic formal types" in that article I can't see one that imposes the exact restriction you want : "any scalar type". Pity... Unless someone has a better idea, I would widen the declaration to:

type Element is private;

然后继续。这可能不是缺点:如果您提供自己的运算符,请参见下一节有关通用形式子程序的

and carry on. This may not be a drawback : see the next section on generic formal subprograms, if you supply your own operators

with function "*" (X, Y: Element) return Element;

 with function "*" (X, Y: Element) return Element is <>;

然后可以实例化记录(复数?)矩阵等的泛型(如果有意义)所以。 是<> 会将现有函数用于已经具有它们的类型(浮点型等)以简化实例化

you can then instantiate the generic for records (complex numbers?) matrices etc if it is meaningful to do so. The "is <>" will use existing functions for types that already have them (Float etc) to simplify the instantiations

(编辑:忘了标量包含枚举,通常对于它来说多项式甚至乘法都不有意义!因此将其扩展为私有可能不是这样的缺点)

(Edit : forgot that scalar includes enumerations, for which polynomials or even multiplication, do not usually make sense! So widening it to "private" may not be such a drawback)

这篇关于在Ada中定义通用标量类型的包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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