comp.lang.ada
 help / color / mirror / Atom feed
* attributes in generic procedure
@ 1999-11-21  0:00 daelen
  1999-11-21  0:00 ` Matthew Heaney
  1999-11-25  0:00 ` Nick Roberts
  0 siblings, 2 replies; 6+ messages in thread
From: daelen @ 1999-11-21  0:00 UTC (permalink / raw)


I would like to create a generic procedure as shown below which will only be
used for scalar types. Does anybody have a hint how to accomplish this?

generic
   type A_Type is private;
procedure Update (Par : in out A_Type; Value : String);
procedure Update (Par : in out A_Type; Value : String) is
begin
   if Value = "+" then
      if Par = A_Type'Last then
         Par := A_Type'First;
      else
         Par := A_Type'Succ (Par);
      end if;
   elsif Value = "++" then
      Par := A_Type'Last;
   elsif Value = "-" then
      if Par = A_Type'First then
         Par := A_Type'Last;
      else
         Par := A_Type'Pred (Par);
      end if;
   elsif Value = "--" then
      Par := A_Type'First;
   elsif Value /= "" then
      Par := A_Type'Value (Value);
   end if;
end Update;


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: attributes in generic procedure
  1999-11-21  0:00 attributes in generic procedure daelen
@ 1999-11-21  0:00 ` Matthew Heaney
  1999-11-25  0:00 ` Nick Roberts
  1 sibling, 0 replies; 6+ messages in thread
From: Matthew Heaney @ 1999-11-21  0:00 UTC (permalink / raw)


In article <818esd$t6l$1@nnrp1.deja.com> , daelen@my-deja.com  wrote:

> I would like to create a generic procedure as shown below which will only be
> used for scalar types. Does anybody have a hint how to accomplish this?

The issue is that there are different classes of scalar type, and
generic formal types differentiate the classes:

generic
  type Integer_Type is range <>;
  type Enum_Or_Int_Type is (<>);
  type Float_Type is digits <>;
  type Fixed_Type is delta <>;
  type Mod_Type is mod <>;



> generic
>    type A_Type is private;
> procedure Update (Par : in out A_Type; Value : String);

It looks like you need First, Last, Pred, Succ.  Import the type as
private (like you already have), but also import constants and
operations on that type:

generic
  type T is private;
  First : in T;
  Last  : in T;
  with function Pred (O : T) return T is <>;
  with function Succ (O : T) return T is <>;
  with function Value (S : String) return T is <>;
procedure Generic_Update (O : in out T; Image : in String);

Now, when a client instantiates the generic, he supplies the attribute
functions defined for the type as generic actuals:

function Update is new Generic_Update
  (T => Integer,
  First => Integer'First,
  Last => Integer'Last,
  Pred => Integer'Pred, ...);

This will work for any scalar type.


> procedure Update (Par : in out A_Type; Value : String) is
> begin
>    if Value = "+" then
>       if Par = A_Type'Last then
>          Par := A_Type'First;
>       else
>          Par := A_Type'Succ (Par);
>       end if;

For example:

  if Value = "+" then
    if Par = Last then
      Par := First;
    else
      Par := Succ (Par);
    end if;



--
Yeah, well, no one was present when the dinosaurs lived, so I guess
their presence in the fossil record is "only a theory." No one was
present when the Grand Canyon was formed either, or the creation of the
solar system, or, or, or. Of course evolution is "only a theory." What
else would it be as a scientific concept?

Skeptic magazine publisher Micheal Shermer, commenting on the recent
decision by the Oklahoma board of education to include a disclaimer on
school science textbooks warning that evolution is "only a theory."

Skeptic web page: http://www.skeptic.com/




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: attributes in generic procedure
  1999-11-21  0:00 attributes in generic procedure daelen
  1999-11-21  0:00 ` Matthew Heaney
@ 1999-11-25  0:00 ` Nick Roberts
  1999-11-29  0:00   ` Niklas Holsti
  1 sibling, 1 reply; 6+ messages in thread
From: Nick Roberts @ 1999-11-25  0:00 UTC (permalink / raw)


daelen@my-deja.com wrote:
> 
> I would like to create a generic procedure as shown below which will only be
> used for scalar types. Does anybody have a hint how to accomplish this?
> 
> generic
>    type A_Type is private;
> ...
> end Update;

The answer is simple:

   generic
      type A_Type is (<>);
   ...

I suspect the poster needs to do a little brushing up on generics.

-- 
Nick Roberts
Computer Consultant (UK)
http://www.adapower.com/lab/adaos





^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: attributes in generic procedure
  1999-11-25  0:00 ` Nick Roberts
@ 1999-11-29  0:00   ` Niklas Holsti
  1999-11-29  0:00     ` Robert Dewar
  0 siblings, 1 reply; 6+ messages in thread
From: Niklas Holsti @ 1999-11-29  0:00 UTC (permalink / raw)


Nick Roberts wrote:
> 
> daelen@my-deja.com wrote:
> >
> > I would like to create a generic procedure as shown below which will only be
> > used for scalar types. Does anybody have a hint how to accomplish this?
> >
> > generic
> >    type A_Type is private;
> > ...
> > end Update;
> 
> The answer is simple:
> 
>    generic
>       type A_Type is (<>);
>    ...

According to RM 12.5.2, this is a formal discrete type definition,
not a formal scalar type definition. This means that it allows only
enumeration and integer types, but not real types (3.2(3)).

In fact, there is no formal_scalar_definition in the RM. I, too, would
have liked to have one when I recently wrote a generic function to
take the maximum value of a vector of some generic element type.
The Max function needs only the attributes 'first and 'max, which
exist for all scalar types, but different generic functions are
needed for a discrete element type and a real element type, although
the bodies of the two functions are identical. One can of course work
around this by making 'first and 'max be additional generic formal
parameters, instead of using type attributes, but it is a little messy.

I don't know if it's worthwhile to consider adding a formal scalar
class to Ada. It might be difficult for compilers that share generic
code across instantiations 

Niklas Holsti
Working at but not speaking for Space Systems Finland Ltd.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: attributes in generic procedure
  1999-11-29  0:00   ` Niklas Holsti
@ 1999-11-29  0:00     ` Robert Dewar
  1999-12-03  0:00       ` Nick Roberts
  0 siblings, 1 reply; 6+ messages in thread
From: Robert Dewar @ 1999-11-29  0:00 UTC (permalink / raw)


In article <3841CE51.862ACED3@icon.fi>,
  Niklas Holsti <nholsti@icon.fi> wrote:
> In fact, there is no formal_scalar_definition in the RM. I,
> too, would have liked to have one when I recently wrote a
> generic function to take the maximum value of a vector of some
> generic element type.

For good reason!

How would one compile shared code for such a case? Answer you
would have to pass in all possible operations. Given that this
is the case, you can achieve this result yourself, either with
a specific list of generic operators, or with a formal generic
package containing the required operations.

The built in formal types in generics are quite deliberately
limited to those that can be handled efficiently in shared
code. Remember that you can always do anything you want with
formal private types by passing in the operations that are
needed.

The built in formal types allow a simplification in the cases
where the set of built in types is the same across a class
of types and therefore do not need to be passed in.


Sent via Deja.com http://www.deja.com/
Before you buy.




^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: attributes in generic procedure
  1999-11-29  0:00     ` Robert Dewar
@ 1999-12-03  0:00       ` Nick Roberts
  0 siblings, 0 replies; 6+ messages in thread
From: Nick Roberts @ 1999-12-03  0:00 UTC (permalink / raw)


Apologies for the mistake. My eyes read 'scalar' but my brain read
'discrete'.

-- 
Nick Roberts





^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~1999-12-03  0:00 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1999-11-21  0:00 attributes in generic procedure daelen
1999-11-21  0:00 ` Matthew Heaney
1999-11-25  0:00 ` Nick Roberts
1999-11-29  0:00   ` Niklas Holsti
1999-11-29  0:00     ` Robert Dewar
1999-12-03  0:00       ` Nick Roberts

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox