comp.lang.ada
 help / color / mirror / Atom feed
* Creating a generic package with and setting its type to that of a record
@ 2016-09-28  1:02 Andrew Shvets
  2016-09-28  2:08 ` Jeffrey R. Carter
  2016-09-28  6:15 ` Simon Wright
  0 siblings, 2 replies; 6+ messages in thread
From: Andrew Shvets @ 2016-09-28  1:02 UTC (permalink / raw)


Lets say I have this code:


generic
  type Custom_Record_Type is <>; 
package Gener is
  ...


What I'd like to do is set the type to that of a record (which has some overloaded operators, such as >, < and =.)  Can this be done?  If so, how?


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

* Re: Creating a generic package with and setting its type to that of a record
  2016-09-28  1:02 Creating a generic package with and setting its type to that of a record Andrew Shvets
@ 2016-09-28  2:08 ` Jeffrey R. Carter
  2016-09-28  2:15   ` Andrew Shvets
  2016-09-28  6:15 ` Simon Wright
  1 sibling, 1 reply; 6+ messages in thread
From: Jeffrey R. Carter @ 2016-09-28  2:08 UTC (permalink / raw)


On 09/27/2016 06:02 PM, Andrew Shvets wrote:
> Lets say I have this code:
> 
> 
> generic
>   type Custom_Record_Type is <>; 
> package Gener is
>   ...
> 
> 
> What I'd like to do is set the type to that of a record (which has some overloaded operators, such as >, < and =.)  Can this be done?  If so, how?

This is an invalid generic formal type definition, so you can't do anything with it.

-- 
Jeff Carter
"I did not rob a bank. If I'd robbed a bank, everything
would be great. I tried to rob a bank, is what happened,
and they got me."
Take the Money and Run
139

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

* Re: Creating a generic package with and setting its type to that of a record
  2016-09-28  2:08 ` Jeffrey R. Carter
@ 2016-09-28  2:15   ` Andrew Shvets
  0 siblings, 0 replies; 6+ messages in thread
From: Andrew Shvets @ 2016-09-28  2:15 UTC (permalink / raw)


On Tuesday, September 27, 2016 at 10:09:01 PM UTC-4, Jeffrey R. Carter wrote:
> On 09/27/2016 06:02 PM, Andrew Shvets wrote:
> > Lets say I have this code:
> > 
> > 
> > generic
> >   type Custom_Record_Type is <>; 
> > package Gener is
> >   ...
> > 
> > 
> > What I'd like to do is set the type to that of a record (which has some overloaded operators, such as >, < and =.)  Can this be done?  If so, how?
> 
> This is an invalid generic formal type definition, so you can't do anything with it.
> 
> -- 
> Jeff Carter
> "I did not rob a bank. If I'd robbed a bank, everything
> would be great. I tried to rob a bank, is what happened,
> and they got me."
> Take the Money and Run
> 139

So if I have this record in another package and it works, I can't use it in my code example?

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

* Re: Creating a generic package with and setting its type to that of a record
  2016-09-28  1:02 Creating a generic package with and setting its type to that of a record Andrew Shvets
  2016-09-28  2:08 ` Jeffrey R. Carter
@ 2016-09-28  6:15 ` Simon Wright
  2016-09-28 21:38   ` Randy Brukardt
  1 sibling, 1 reply; 6+ messages in thread
From: Simon Wright @ 2016-09-28  6:15 UTC (permalink / raw)


Andrew Shvets <andrew.shvets@gmail.com> writes:

> generic
>   type Custom_Record_Type is <>; 
> package Gener is
>   ...
>
> What I'd like to do is set the type to that of a record (which has
> some overloaded operators, such as >, < and =.)  Can this be done?  If
> so, how?

I think you are looking for generic formal subprograms[1]. Something
like

generic
   type Custom_Record_Type is private;
   with function ">" (L, R; Custom_Record_Type) return Boolean is <>;
   with function "<" (L, R; Custom_Record_Type) return Boolean is <>;
   with function "=" (L, R; Custom_Record_Type) return Boolean is <>;
package Gener is

The 'is <>' notation means you don't need to supply an actual for this
parameter if a matching one is visible at the point of instantiation.

You could instantiate it for Integer, but I'm not sure what operation
would be used if you tried "<=", for instance; the composition of your
generic actuals, or the emergent "<=" of Integer.

[1] https://en.wikibooks.org/wiki/Ada_Programming/Generics#Generic_formal_subprograms


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

* Re: Creating a generic package with and setting its type to that of a record
  2016-09-28  6:15 ` Simon Wright
@ 2016-09-28 21:38   ` Randy Brukardt
  2016-09-29  7:57     ` Simon Wright
  0 siblings, 1 reply; 6+ messages in thread
From: Randy Brukardt @ 2016-09-28 21:38 UTC (permalink / raw)


"Simon Wright" <simon@pushface.org> wrote in message 
news:ly60pg5z3x.fsf@pushface.org...
...
> You could instantiate it for Integer, but I'm not sure what operation
> would be used if you tried "<=", for instance; the composition of your
> generic actuals, or the emergent "<=" of Integer.

Neither. "<=" would be undefined (inside the generic) with the specification 
you gave. Outside the generic, you of course would use Integer's operators 
(because they're visible). You never have visibility on formal subprograms 
outside of the generic (that is, via the instance).

                              Randy.



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

* Re: Creating a generic package with and setting its type to that of a record
  2016-09-28 21:38   ` Randy Brukardt
@ 2016-09-29  7:57     ` Simon Wright
  0 siblings, 0 replies; 6+ messages in thread
From: Simon Wright @ 2016-09-29  7:57 UTC (permalink / raw)


"Randy Brukardt" <randy@rrsoftware.com> writes:

> "Simon Wright" <simon@pushface.org> wrote in message 
> news:ly60pg5z3x.fsf@pushface.org...
> ...
>> You could instantiate it for Integer, but I'm not sure what operation
>> would be used if you tried "<=", for instance; the composition of
>> your generic actuals, or the emergent "<=" of Integer.
>
> Neither. "<=" would be undefined (inside the generic) with the
> specification you gave. Outside the generic, you of course would use
> Integer's operators (because they're visible). You never have
> visibility on formal subprograms outside of the generic (that is, via
> the instance).

Of course - thanks, Randy.

I was confused, I suppose, by "/=" vs "=" [ARM 6.6(6)]

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

end of thread, other threads:[~2016-09-29  7:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-09-28  1:02 Creating a generic package with and setting its type to that of a record Andrew Shvets
2016-09-28  2:08 ` Jeffrey R. Carter
2016-09-28  2:15   ` Andrew Shvets
2016-09-28  6:15 ` Simon Wright
2016-09-28 21:38   ` Randy Brukardt
2016-09-29  7:57     ` Simon Wright

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