Adding a "Project Types" layer to a project 
Author Message
 Adding a "Project Types" layer to a project

I'm beginning a large project, and I'd like to base the entire project off base types defined in a package, as an extra layer of indirection for which I can
change the underlying functionality later.  The problem is, I'm having problems adding this level of indirection.  Keep in mind I know very little about Ada 95,
save for what I'm picking up here on the newsgroups and such...

 Here's what I have so far.  Note that I want everything in the project to use ONLY the types defined in the package "types", which I can change to affect the
entire project.  This is the result I get when I try to compile this testcase:

/home/chrispy/bot > gnatmake test
gcc -c test.adb
test.adb:20:20: invalid operand types for operator "+"
gnatmake: *** compilation failed.

 If I use the type from the package "vectors" directly, it works.  If I create a subtype in types, and then use that, it fails.  What am I missing?  Thanks in
advance...

 - Chris

===== vectors.ads:
package VECTORS is
  type VECTOR is array(integer range<>) of float ;

  function "+" (A : VECTOR; B : VECTOR) return VECTOR ; -- sum of vector
end VECTORS;

===== vectors.adb:
package body VECTORS is
  function "+" (A : VECTOR; B : VECTOR) return VECTOR is
    C : VECTOR(A'first..A'last) ;
  begin
    if A'first /= B'first or A'last /= B'last then
      raise INCOMPARABLE_DIMENSION;
    end if ;
    for I in A'range loop
      C(I) := A(I)+B(I) ;
    end loop ;
    return C ;
  end "+";
end VECTORS;

===== types.ads:
with Vectors;

package TYPES is
  subtype Distance is FLOAT;
  subtype Vector is Vectors.VECTOR;
end TYPES;

===== test.adb:
with Text_IO, Ada.Float_Text_IO, Ada.Integer_Text_IO;
with Types;

use Text_IO, Ada.Float_Text_IO, Ada.Integer_Text_IO;
use Types;

procedure test is

distance1: Distance := 2.0;
distance2: Distance := 3.0;
point1: Vector := (1.1, 2.2);
point2: Vector := (3.3, 4.4);

begin
  distance1 := distance1 + distance2;
  point1 := point1 + point2;
end;



Sun, 18 Oct 1998 03:00:00 GMT  
 Adding a "Project Types" layer to a project



Quote:
> If I use the type from the package "vectors" directly, it works.  If I
>create a subtype in types, and then use that, it fails.  What am I
>missing?  Thanks in advance...

There is no "+" declared in package Types.  Hence the error message.
The "+" you want to call is declared in Vectors, and it has to be
directly visible in order to call it.

You could rename the "+" operator into Types.

Or, you could say "use type Vector;" in Test.

Or, you could use a derived type instead of a subtype.  This will create
a new type, with an inherited "+", which may or may not be what you
want.

- Bob

P.S. I don't particularly like the idea of having a Types package, which
is just a big jumble of totally unrelated stuff.



Sun, 18 Oct 1998 03:00:00 GMT  
 Adding a "Project Types" layer to a project

: /home/chrispy/bot > gnatmake test
: gcc -c test.adb
: test.adb:20:20: invalid operand types for operator "+"
: gnatmake: *** compilation failed.

:  If I use the type from the package "vectors" directly, it works.  If I create a subtype in types, and then use that, it fails.  What am I missing?  Thanks in
: advance...

You'll have to provide a renaming of "+" in Types, or "with Vectors" and
"use [type] Vectors" in the main program.  "+" is in Vectors but not in
Types, so the program doesn't know anything about it.

--
----------------------------------------------------------------------------

  "Disks are divided into sex and tractors..."
----------------------------------------------------------------------------



Sun, 18 Oct 1998 03:00:00 GMT  
 Adding a "Project Types" layer to a project

Quote:

> I'm beginning a large project, and I'd like to base the entire project off base types defined in a package, as an extra layer of indirection for which I can
> change the underlying functionality later.  The problem is, I'm having problems adding this level of indirection.  Keep in mind I know very little about Ada 95
> save for what I'm picking up here on the newsgroups and such...
> I would hesitate a bit to base everything on a "base types" package.  Although it seems like a good

idea at first, when you are in the small scale, as the project gets larger, the initially simple
types package gets more and more difficult to manage.

We have to keep in mind the "object oriented way."  Supposedly, the data and the function are
together.  What you have shown with your software problem is that by taking the vector functions and
putting them in another package, they are not available when you use the vector type later on.  If
you are using vectors, use the vectors package.  The indirection provided by the types package seems
to confuse the problem.

I'm not saying that a "standard" types package is not a good thing.  There are times when it may
make more sense that you might consider using a types package.  In those instances, you may consider
limiting the types package to be within the subsystem you are using.  

cb



Mon, 19 Oct 1998 03:00:00 GMT  
 Adding a "Project Types" layer to a project

 Thank you everyone for your replies!

 I think I was a bit over-eager to use this "project types" concept.
Chad is definitely right, for the project I'm working on, it's better
to just use the vectors package if I need vectors functionality.  I
was trying to offer as much flexibility and indirection as I could for
the groundwork, but it appears to be superfluous now that I look at
this instance.

 - Chris

Quote:

>We have to keep in mind the "object oriented way."  Supposedly, the data and the function are
>together.  What you have shown with your software problem is that by taking the vector functions and
>putting them in another package, they are not available when you use the vector type later on.  If
>you are using vectors, use the vectors package.  The indirection provided by the types package seems
>to confuse the problem.
>I'm not saying that a "standard" types package is not a good thing.  There are times when it may
>make more sense that you might consider using a types package.  In those instances, you may consider
>limiting the types package to be within the subsystem you are using.  
>cb



Fri, 23 Oct 1998 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. "Contract VisualAge-Project Lead"

2. Projects for "5th and 6th graders"

3. Need an "exceptional" public VHDL project

4. "Simple" Prolog Interpreter Project

5. "Compiler" 1.2 - Project Setup available

6. "Real" Projects in Smalltalk

7. AAAhhhh!!!!...."Impossible" to purge several layers

8. Looking for canvas-widget with "layers"

9. No Boolean type in "types"

10. string.join(["Tk 4.2p2", "Python 1.4", "Win32", "free"], "for")

11. "[jobs] contract-Project Manager-Ea

12. Adding menu to mac "system tray"

 

 
Powered by phpBB® Forum Software