艾达:如何解决“循环单位依赖性”? [英] Ada: how to solve "Circular Unit Dependency"?

查看:77
本文介绍了艾达:如何解决“循环单位依赖性”?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有两条记录: Person Animal 。每条记录都放在单独的包装中。

Suppose I have two records: Person and Animal. Each record is in a separate package.

包装人员:

with animals;
use animals;

package persons is 

    type person is record
     ...
     animalref: animalPOINTER;
     ...
    end record;

    type personPOINTER is access person;

end persons;

包装动物:

with persons;
use persons;

package animals is 
    type animal is record
     ...
     ownerref:  personPOINTER;
     ...
    end record;

    type animalPOINTER is access animal;

end animals;

我在这里有循环单位依赖性,编译器会产生致命错误。

I have Circular Unit Dependency here, and compiler produces fatal error.

有人能解决这种问题吗?

Does anyone have a pattern to address such issue ?

谢谢!

推荐答案

您需要限制为,这是为解决此问题而引入的。请参见 Ada 2005原理第4.2节 a>。

You need limited with, which was introduced to address exactly this problem. See the Rationale for Ada 2005, section 4.2.

动物是对称的(我的编辑器调整了布局和大小写;我向每个添加了一个记录组件,以便下面的演示程序可以打印一些内容):

Animals and Persons are symmetric (my editor has adjusted the layout and casing; I’ve added one record component to each so the demo program, below, can print something):

limited with Animals;
package Persons is

   --  One of the few things you can do with an incomplete type, which
   --  is what Animals.Animal is in the limited view of Animals, is to
   --  declare an access to it.
   type AnimalPOINTER is access Animals.Animal;

   type Person is record
      Name : Character;
      Animalref : AnimalPOINTER;
   end record;

end Persons;

limited with Persons;
package Animals is

   type PersonPOINTER is access Persons.Person;

   type Animal is record
      Name : Character;
      Ownerref : PersonPOINTER;
   end record;

end Animals;

该演示程序具有动物和人员。这个例子很笨拙。您可以通过将子程序添加到动物中来更好地组织事情。请注意,动物 body 可以(并且必须)与Persons一起(如果需要)

The demo program has the full view of Animals and Persons. This example is pretty clumsy; you may be able to organise things better by adding subprograms to Animals and Persons. Note that the body of Animals can (and must) with Persons; if it needs to use anything in Persons.

with Ada.Text_IO; use Ada.Text_IO;
with Animals;
with Persons;
procedure Animals_And_Persons is
   A : Persons.animalPOINTER := new Animals.Animal;
   P : Animals.PersonPOINTER := new Persons.Person;
begin
   A.all := (Name => 'a', Ownerref => P);
   P.all := (Name => 'p', Animalref => A);
   Put_Line (P.Name & " owns " & P.Animalref.Name);
   Put_Line (A.Name & " is owned by " & A.Ownerref.Name);
end Animals_And_Persons;

在编译和运行时给出的结果

which when compiled and run gives

$ ./animals_and_persons 
p owns a
a is owned by p

这篇关于艾达:如何解决“循环单位依赖性”?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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