[comp.lang.ada] Questions on Ada...

tfrancis@wpi.wpi.edu (Krishan M Nainani) (06/29/89)

	I have just started learning Ada and have a few questions on it.

(1)	Is there any way to mimic the C pre-processor commands in Ada.
	For example, is it possible to mimic:
	#ifdef TEMP
	...
	#endif

(2)	Is it possible to copy sections of memory regardless of its
	contents from one type to another. For example, in C:

	atype : *ptr1; btype : *ptr2; /* atype and btype are different */
	for (i=0;i<123;i++)  *ptr1++ = *ptr2++; 


Thanx in advance.

Krishan Nainani.	reply-to: tfrancis@wpi.wpi.edu

Cheers !

arny@cbnewsl.ATT.COM (arny.b.engelson) (07/03/89)

In article <3034@wpi.wpi.edu> tfrancis@wpi.wpi.edu (Krishan M Nainani) writes:
>
>(1)	Is there any way to mimic the C pre-processor commands in Ada.
>	For example, is it possible to mimic:
>	#ifdef TEMP
>	...
>	#endif

Well, sort of.  It is really outside of the language, but you can use the
same C pre-processor commands, and simply run the code through the C pre-
processor before running it through the Ada compiler.  I have actually seen
people do this, and it has worked quite well.  It may not be so hot from
a Configuration Management point of view, and will get some Ada purists up
in arms, but it works.

On some compilers you may be able to achieve this using Pragmas, keeping
the code true Ada, but unusable on other systems.


>(2)	Is it possible to copy sections of memory regardless of its
>	contents from one type to another. For example, in C:
>
>	atype : *ptr1; btype : *ptr2; /* atype and btype are different */
>	for (i=0;i<123;i++)  *ptr1++ = *ptr2++; 

There is more than one way to do this.  One alternative is to use overlays.
Simply declare an integer array of the proper length, use an address clause
to overlay it at the location occupied by the structure you are copying
from, and copy it to an integer array overlayed at the destination address.
Another alternative is to use unchecked conversion from one type to another.
Again, the Ada purists will be up in arms (overlays are officially erroneous),
but the concept is very useful in some situations.  BTW, erroneous doesn't
mean it won't work, but it does mean you had better be sure you know what you
are doing.

>Thanx in advance.
You're welcome.

>Krishan Nainani.	reply-to: tfrancis@wpi.wpi.edu

  - Arny Engelson   wayback!arny

johnro@microsoft.UUCP (John Rogers) (07/06/89)

Hi!  There is another, more standard, way of doing conditional compilation
in Ada.  The original article had something like:

#ifdef SOMETHING
    random_C_code;
#endif

In Ada, it's possible to have groups of statements that are never
executed.  For instance:

   procedure Example is
     Something : constant Boolean := True;    -- change and recompile...
   begin
     if (Something) then
       random_Ada_Code;
     end if;
   end Example;

If "Something" is set to False in some other environment, then the
statements ("random_Ada_Code") will never be executed.  The ANSI/MIL-STD
for Ada allows this, and allows compilers to completely optimize out the
code.  This acts as a sort of conditional compilation.

Unfortunately, this isn't a complete solution to the need for
conditional compilation.  There's no way to do this for data declarations,
or "with" clauses, or various other things - just executable statements.

Hope this helps...

vladimir@inteloc.intel.com (Vladimir G. Ivanovic) (07/07/89)

In article <979@cbnewsl.ATT.COM> arny@cbnewsl.ATT.COM (arny.b.engelson) writes:
>There is more than one way to do this.  One alternative is to use overlays.
>Simply declare an integer array of the proper length, use an address clause
>to overlay it at the location occupied by the structure you are copying
>from, and copy it to an integer array overlayed at the destination address.
>Another alternative is to use unchecked conversion from one type to another.
>Again, the Ada purists will be up in arms (overlays are officially erroneous),
>but the concept is very useful in some situations.

I agree about the utility, but disagree about the "officially erroneous"
comment attributed to Ada purists.  In fact, I'd say there are entire classes
of problems that cannot be solved without using Unchecked_Conversion. 

A more understanding approach to strong typing, Ada and program construction
stresses the utility of letting the compiler do as much work for the
programmer as possible.  Heck, I'd buy in a flash a system that allowed me to
specify a problem domain complete with hems and haws and retractions and
contradictions inherent in unstructured human thought.

The problem with using Unchecked_Conversion is that the programmer is saying
to the compiler, "Trust me.  I really do know what I'm doing."   The compiler
can't therefore check for consistency by asking "Does this make sense?" i.e.
by checking types.

Time and time again, experienced Ada programmers say that it takes a fair bit
of time before a person begins to think Ada-think.  In other words, it takes
time (and understanding) before programmers learn to organize their programs
as a collection of objects (data structures with associated methods =
packages) passing messages (procedure calls and rendezvous).  

The original posting asking "How can I translate this <insert favorite
langauge> into Ada?" is a stage all Ada programmers go through.  There is a
better way, and it requires a different mindset.

arny@cbnewsl.ATT.COM (arny.b.engelson) (07/12/89)

In article <4611@omepd.UUCP> vladimir@inteloc.UUCP (Vladimir G. Ivanovic) writes:
>In article <979@cbnewsl.ATT.COM> arny@cbnewsl.ATT.COM (arny.b.engelson) writes:
>>There is more than one way to do this.  One alternative is to use overlays.
>>Simply declare an integer array of the proper length, use an address clause
>>to overlay it at the location occupied by the structure you are copying
>>from, and copy it to an integer array overlaid at the destination address.
>>Another alternative is to use unchecked conversion from one type to another.
>>Again, the Ada purists will be up in arms (overlays are officially erroneous),
>>but the concept is very useful in some situations.

>I agree about the utility, but disagree about the "officially erroneous"
>comment attributed to Ada purists.  In fact, I'd say there are entire classes
>of problems that cannot be solved without using Unchecked_Conversion. 

Read my original posting more carefully.  I did not say that Unchecked_
Conversion was erroneous.  I said overlays are erroneous, and they do
not use U_C.  Read LRM 13.5:8.

  - Arny Engelson   att!wayback!arny