1

Let's say I have a generic vector library. To make it easier to use, I want to instantiate various common forms of the vector library and make them visible in a single package.

I'm trying this:

with GenericVector;

package Vectors is
    package Vectors3 is new GenericVector(3);
    use all type Vectors3.Vector;
    subtype Vector3 is Vectors3.Vector;

    package Vectors4 is new GenericVector(4);
    use all type Vectors4.Vector;
    subtype Vector4 is Vectors4.Vector;
end;

The end goal is that I want to be able to do with Vectors; use Vectors; and end up with Vector3 and Vector4 types directly available which Just Work.

Naturally, the code above doesn't work. It looks like the use all type statements import the definitions attached to the specified type into the package specification but then those definitions aren't exported to the user of Vectors. I have to do with Vectors; use Vectors; use all type Vectors.Vectors3; instead. This is kind of sucky.

How can I do this?

4

2 回答 2

1

您可以简单地创建Vector3Vector4类型,而不仅仅是子类型。GenericVector这将隐式声明来自in的所有继承的原始操作Vectors

于 2014-05-18T07:52:10.133 回答
1

use Vectors使您可以直接看到在 中声明的那些标识符Vectors,包括那些隐式声明的标识符。(隐式声明是声明新整数类型时的"+", "-", 运算符,以及声明派生类型时的继承操作。)但它不会让您直接看到其他任何内容。特别是,use不是可传递的,因为use all type Vectors3.Vector没有在 中声明任何新的标识符Vectors

use Vectors您可以通过为您希望用户看到的所有内容声明重命名标识符来完成此操作。(对于类型,您必须使用,subtype因为 Ada 中没有类型重命名。)例如Vectors

function Dot_Product (V1, V2 : Vectors3.Vector) return Float
   renames Vectors3.Dot_Product;

(我只是猜测其中的操作GenericVectors可能是什么样子。)现在,任何地方use Vectors都可以Dot_Product直接使用。不过,您必须为每个标识符做这样的事情。如果它们很多,这可能不是一个可行的解决方案。(重命名声明不必使用相同的名称Dot_Product。)

尽管您无法获得这种传递可见性可能看起来很烦人,但替代方案可能会变得更烦人,因为您无法查看Vectors和查看哪些标识符会被use Vectors; 结果可能是意外的名称冲突或其他意外。

于 2014-05-19T15:53:40.397 回答