* User define attributes @ 2001-04-14 13:33 Ayende Rahien 2001-04-16 7:27 ` Martin Dowie 0 siblings, 1 reply; 20+ messages in thread From: Ayende Rahien @ 2001-04-14 13:33 UTC (permalink / raw) How do I add attributes to a type I create? And how do I require certain attributes to be exist in a type? (I want a generic package to accept only types that has 'Image attribute) Thanks in advance, Ayende Rahien ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-14 13:33 User define attributes Ayende Rahien @ 2001-04-16 7:27 ` Martin Dowie 2001-04-16 11:23 ` Ayende Rahien 0 siblings, 1 reply; 20+ messages in thread From: Martin Dowie @ 2001-04-16 7:27 UTC (permalink / raw) don't think you can. I believe 'Image is available for all scalar types. you could do something like generic type Blah is private; function Image (X : Blah) return String; package <...>; Ayende Rahien <Dont@spam.me> wrote in message news:9b9g7c$cp6$1@taliesin.netcom.net.uk... > How do I add attributes to a type I create? > > And how do I require certain attributes to be exist in a type? (I want a > generic package to accept only types that has 'Image attribute) > > Thanks in advance, > Ayende Rahien > > ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 7:27 ` Martin Dowie @ 2001-04-16 11:23 ` Ayende Rahien 2001-04-16 11:37 ` Martin Dowie 0 siblings, 1 reply; 20+ messages in thread From: Ayende Rahien @ 2001-04-16 11:23 UTC (permalink / raw) Okay, what abuot defining my own? "Martin Dowie" <martin.dowie@nospam.baesystems.com> wrote in message news:3ada9cf5$1@pull.gecm.com... > don't think you can. I believe 'Image is available for all scalar types. > you could do something like > > generic > type Blah is private; > function Image (X : Blah) return String; > package <...>; > > > Ayende Rahien <Dont@spam.me> wrote in message > news:9b9g7c$cp6$1@taliesin.netcom.net.uk... > > How do I add attributes to a type I create? > > > > And how do I require certain attributes to be exist in a type? (I want a > > generic package to accept only types that has 'Image attribute) > > > > Thanks in advance, > > Ayende Rahien > > > > > > ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 11:23 ` Ayende Rahien @ 2001-04-16 11:37 ` Martin Dowie 2001-04-16 14:35 ` Samuel T. Harris 0 siblings, 1 reply; 20+ messages in thread From: Martin Dowie @ 2001-04-16 11:37 UTC (permalink / raw) well, if you mean someting like type My_Type ...; for My_Type'Image use ...; you can't (that's for stream read/write only... I think), but as attributes are defined as functions anyway (check annex L) you can force your generic to require an Image function and even do nifty instantiations like... generic type My_Type is private; with function Image (X : My_Type) return String; package Test is procedure Call (X : My_Type); end Test; with Test; package Tester is new test (Integer, Integer'Image); For composite types you have to "roll your own" Image routine that would probably just call up the scalar 'Image routines in turn or other rolled composite IMage routines. Ayende Rahien <Dont@spam.me> wrote in message news:9behk1$ik9$1@taliesin.netcom.net.uk... > Okay, what abuot defining my own? > > "Martin Dowie" <martin.dowie@nospam.baesystems.com> wrote in message > news:3ada9cf5$1@pull.gecm.com... > > don't think you can. I believe 'Image is available for all scalar types. > > you could do something like > > > > generic > > type Blah is private; > > function Image (X : Blah) return String; > > package <...>; > > > > > > Ayende Rahien <Dont@spam.me> wrote in message > > news:9b9g7c$cp6$1@taliesin.netcom.net.uk... > > > How do I add attributes to a type I create? > > > > > > And how do I require certain attributes to be exist in a type? (I want a > > > generic package to accept only types that has 'Image attribute) > > > > > > Thanks in advance, > > > Ayende Rahien > > > > > > > > > > > > ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 11:37 ` Martin Dowie @ 2001-04-16 14:35 ` Samuel T. Harris 2001-04-16 15:20 ` Martin Dowie 2001-04-18 12:14 ` Marius Amado Alves 0 siblings, 2 replies; 20+ messages in thread From: Samuel T. Harris @ 2001-04-16 14:35 UTC (permalink / raw) Martin Dowie wrote: > > well, if you mean someting like > > type My_Type ...; > for My_Type'Image use ...; > > you can't (that's for stream read/write only... I think), but as > attributes are defined as functions anyway (check annex L) you can > force your generic to require an Image function and even do nifty > instantiations like... > > generic > type My_Type is private; > with function Image (X : My_Type) return String; > package Test is > procedure Call (X : My_Type); > end Test; > > with Test; > package Tester is new test (Integer, Integer'Image); > > For composite types you have to "roll your own" Image routine that > would probably just call up the scalar 'Image routines in turn > or other rolled composite IMage routines. > It is trivial to build a generic which implements the a width, value, and image function for arrays. All it needs are the width, value, and image functions for the indexes and composite types. It is somewhat harder to build a generic for records since the "index" are the field names and the composite function depend upon which field is in use. But it can be done. I routinely write width, value, and image functions for all my important types. Having these generics in place, I get these routines with some simple handler code and a generic instantiations. With them I get the following advantages ... 1. I can easily insert put_line kinds of debug code. 2. If supported, I can call them within my debugger. 3. I can defer aggregrate initialization of variables to file_io during elaboration, and vice-versa. -- Samuel T. Harris, Principal Engineer Raytheon, Aerospace Engineering Services "If you can make it, We can fake it!" ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 14:35 ` Samuel T. Harris @ 2001-04-16 15:20 ` Martin Dowie 2001-04-16 15:37 ` Brian Rogoff ` (2 more replies) 2001-04-18 12:14 ` Marius Amado Alves 1 sibling, 3 replies; 20+ messages in thread From: Martin Dowie @ 2001-04-16 15:20 UTC (permalink / raw) been there; done that; got the compiler listing... :-) I suspect there are many companies out there with very similar generics. I suppose we have to ask the question - if it is that easy (and relative to the actual application, it usually is!) then why isn't 'Image for composite types part of the language?.. Samuel T. Harris <u61783@gsde.hou.us.ray.com> wrote in message news:3ADB033A.F10344A9@gsde.hou.us.ray.com... > It is trivial to build a generic which implements the a > width, value, and image function for arrays. All it needs > are the width, value, and image functions for the indexes > and composite types. > > It is somewhat harder to build a generic for records > since the "index" are the field names and the composite > function depend upon which field is in use. But it > can be done. > > I routinely write width, value, and image functions > for all my important types. Having these generics > in place, I get these routines with some simple > handler code and a generic instantiations. With them > I get the following advantages ... > > 1. I can easily insert put_line kinds of debug code. > 2. If supported, I can call them within my debugger. > 3. I can defer aggregrate initialization of variables > to file_io during elaboration, and vice-versa. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 15:20 ` Martin Dowie @ 2001-04-16 15:37 ` Brian Rogoff 2001-04-16 16:19 ` Samuel T. Harris [not found] ` <0tEC6.7646$FY5.638172@www.newsranger.com> 2 siblings, 0 replies; 20+ messages in thread From: Brian Rogoff @ 2001-04-16 15:37 UTC (permalink / raw) On Mon, 16 Apr 2001, Martin Dowie wrote: > been there; done that; got the compiler listing... :-) :-) > I suspect there are many companies out there with very > similar generics. > > I suppose we have to ask the question - if it is that > easy (and relative to the actual application, it > usually is!) then why isn't 'Image for composite types > part of the language?.. Given the desire to make array and function call syntax identical, I wonder why there is even an attribute syntax at all, rather than just having function call syntax and system defined functions. I'd much rather just write Image(X) everywhere (but write A[I] for array access :). -- Brian > Samuel T. Harris <u61783@gsde.hou.us.ray.com> wrote in message > news:3ADB033A.F10344A9@gsde.hou.us.ray.com... > > It is trivial to build a generic which implements the a > > width, value, and image function for arrays. All it needs > > are the width, value, and image functions for the indexes > > and composite types. > > > > It is somewhat harder to build a generic for records > > since the "index" are the field names and the composite > > function depend upon which field is in use. But it > > can be done. > > > > I routinely write width, value, and image functions > > for all my important types. Having these generics > > in place, I get these routines with some simple > > handler code and a generic instantiations. With them > > I get the following advantages ... > > > > 1. I can easily insert put_line kinds of debug code. > > 2. If supported, I can call them within my debugger. > > 3. I can defer aggregrate initialization of variables > > to file_io during elaboration, and vice-versa. > > > > ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 15:20 ` Martin Dowie 2001-04-16 15:37 ` Brian Rogoff @ 2001-04-16 16:19 ` Samuel T. Harris 2001-04-16 17:08 ` Marin David Condic 2001-04-16 17:37 ` Martin Dowie [not found] ` <0tEC6.7646$FY5.638172@www.newsranger.com> 2 siblings, 2 replies; 20+ messages in thread From: Samuel T. Harris @ 2001-04-16 16:19 UTC (permalink / raw) Martin Dowie wrote: > > been there; done that; got the compiler listing... :-) > > I suspect there are many companies out there with very > similar generics. > > I suppose we have to ask the question - if it is that > easy (and relative to the actual application, it > usually is!) then why isn't 'Image for composite types > part of the language?.. > I believe I asked that very same question here some years ago. The response I got was the difficulty in defining such a thing within the language and the difficulties in implementing it in general. To which I countered that the very same issues are relevant to the definition and implementation of streams. That is as far as the thread went (as far as my imperfect memory recalls). -- Samuel T. Harris, Principal Engineer Raytheon, Aerospace Engineering Services "If you can make it, We can fake it!" ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 16:19 ` Samuel T. Harris @ 2001-04-16 17:08 ` Marin David Condic 2001-04-16 22:00 ` Samuel T. Harris 2001-04-16 17:37 ` Martin Dowie 1 sibling, 1 reply; 20+ messages in thread From: Marin David Condic @ 2001-04-16 17:08 UTC (permalink / raw) It seems reasonable to me to say that if you have to provide a translation of a compound type into some stream of bytes (degenerating to the collective internal representations of the primitive types plus index/discriminant info) that adding a similar character representation ought to be near trivial. When you get down to the bottom layer, instead of binary, you translate to ASCII. Coming back in might be more difficult if you expect the input to be user forgiving, but that shouldn't be any worse than calling the 'Value routines for the primitive types. Or an implementation is free to say that the only thing guaranteed to work is Typename'Value(Typename'Image(X))? Personally, I'd be at least mostly happy if the requirement were to give me a dump of a compound structure from byte zero to byte N (plus the dope information?) in various formats. (ASCII, hex, integer, etc.) This would enable dumping the stuff out for debugging purposes which is usually the reason I'd want a 'Image attribute for them anyway. (Rolling your own isn't hard once you've got the object into a stream, but why not go for a convenience feature?) Most "real" outputs would want to be formatted in far more detail than I could ever expect to get from a 'Image attribute anyway, so I wouldn't need it except as a quick and dirty mechanism to see what is in the structure. It is certainly handy for student-level programs. ('Image and 'Value are a lot easier to explain than Text_IO when you just want to get someone rolling with a "Hello World" program.) Or possibly for outputing a compound object to a text file to be read back in at a later point? But then why not just use Streams? (Maybe there should be a "Typename'Dump (X)" attribute with no requirement other than the implementation provide some kind of human readable text for the contents of X?) MDC -- Marin David Condic Senior Software Engineer Pace Micro Technology Americas www.pacemicro.com Enabling the digital revolution e-Mail: marin.condic@pacemicro.com Web: http://www.mcondic.com/ "Samuel T. Harris" <u61783@gsde.hou.us.ray.com> wrote in message news:3ADB1BAA.6F4E0437@gsde.hou.us.ray.com... > I believe I asked that very same question here some > years ago. The response I got was the difficulty in > defining such a thing within the language and the > difficulties in implementing it in general. To which > I countered that the very same issues are relevant > to the definition and implementation of streams. > That is as far as the thread went (as far as my > imperfect memory recalls). > ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 17:08 ` Marin David Condic @ 2001-04-16 22:00 ` Samuel T. Harris 2001-04-17 12:34 ` Georg Bauhaus 0 siblings, 1 reply; 20+ messages in thread From: Samuel T. Harris @ 2001-04-16 22:00 UTC (permalink / raw) Marin David Condic wrote: > > It seems reasonable to me to say that if you have to provide a translation > of a compound type into some stream of bytes (degenerating to the collective > internal representations of the primitive types plus index/discriminant > info) that adding a similar character representation ought to be near > trivial. When you get down to the bottom layer, instead of binary, you > translate to ASCII. Coming back in might be more difficult if you expect the > input to be user forgiving, but that shouldn't be any worse than calling the > 'Value routines for the primitive types. Or an implementation is free to say > that the only thing guaranteed to work is Typename'Value(Typename'Image(X))? My functions, and my original question, dealt with Ada's aggregate with named notation. Specifically qualified aggregates where the full type name and a "'" preceeds the aggregate. > > Personally, I'd be at least mostly happy if the requirement were to give me > a dump of a compound structure from byte zero to byte N (plus the dope > information?) in various formats. (ASCII, hex, integer, etc.) This would > enable dumping the stuff out for debugging purposes which is usually the > reason I'd want a 'Image attribute for them anyway. (Rolling your own isn't > hard once you've got the object into a stream, but why not go for a > convenience feature?) Of course, aggregates using named notation have no need for dope vectors. Record discriminants come first so no problem there. By using qualified aggregates, even complications introduced by tagged types are trivially handled. > > Most "real" outputs would want to be formatted in far more detail than I > could ever expect to get from a 'Image attribute anyway, so I wouldn't need > it except as a quick and dirty mechanism to see what is in the structure. It > is certainly handy for student-level programs. ('Image and 'Value are a lot > easier to explain than Text_IO when you just want to get someone rolling > with a "Hello World" program.) Or possibly for outputing a compound object > to a text file to be read back in at a later point? But then why not just > use Streams? (Maybe there should be a "Typename'Dump (X)" attribute with no > requirement other than the implementation provide some kind of human > readable text for the contents of X?) I don't need "real" outputs. I just want convenient output for anything I want, in function form suitable to be called directly from the debugger. By standardizing on Ada aggregate notation, I can even switch output with source code and source code as input. I even handle pointers in a portable manner. One of the inconveniences in my experience with using streams or other binary representation for transmission is that I cannot simply use a network sniffer to spoof the contents. If I'm passing all data packets with textual contents, I can read what a distributed system is doing with any old spoofer. -- Samuel T. Harris, Principal Engineer Raytheon, Aerospace Engineering Services "If you can make it, We can fake it!" ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 22:00 ` Samuel T. Harris @ 2001-04-17 12:34 ` Georg Bauhaus 2001-04-17 20:35 ` Samuel T. Harris 2001-04-24 21:00 ` Samuel T. Harris 0 siblings, 2 replies; 20+ messages in thread From: Georg Bauhaus @ 2001-04-17 12:34 UTC (permalink / raw) Samuel T. Harris (u61783@gsde.hou.us.ray.com) wrote: : One of the inconveniences in my experience with using streams : or other binary representation for transmission is that I : cannot simply use a network sniffer to spoof the contents. : If I'm passing all data packets with textual contents, I can : read what a distributed system is doing with any old spoofer. So you could write your own 'Write or 'Output and use text streams. Am I missing something? Georg ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-17 12:34 ` Georg Bauhaus @ 2001-04-17 20:35 ` Samuel T. Harris 2001-04-24 21:00 ` Samuel T. Harris 1 sibling, 0 replies; 20+ messages in thread From: Samuel T. Harris @ 2001-04-17 20:35 UTC (permalink / raw) Georg Bauhaus wrote: > > Samuel T. Harris (u61783@gsde.hou.us.ray.com) wrote: > > : One of the inconveniences in my experience with using streams > : or other binary representation for transmission is that I > : cannot simply use a network sniffer to spoof the contents. > : If I'm passing all data packets with textual contents, I can > : read what a distributed system is doing with any old spoofer. > > So you could write your own 'Write or 'Output and > use text streams. Am I missing something? My prior effort was for Ada 83, no streams. While what you suggest would work, there is a lot more overhead to setting up streams. And it doesn't address my original question, if the mechanism was in place to define streams, why is it not in place for image/value/width? -- Samuel T. Harris, Principal Engineer Raytheon, Aerospace Engineering Services "If you can make it, We can fake it!" ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-17 12:34 ` Georg Bauhaus 2001-04-17 20:35 ` Samuel T. Harris @ 2001-04-24 21:00 ` Samuel T. Harris 1 sibling, 0 replies; 20+ messages in thread From: Samuel T. Harris @ 2001-04-24 21:00 UTC (permalink / raw) Georg Bauhaus wrote: > > Samuel T. Harris (u61783@gsde.hou.us.ray.com) wrote: > > : One of the inconveniences in my experience with using streams > : or other binary representation for transmission is that I > : cannot simply use a network sniffer to spoof the contents. > : If I'm passing all data packets with textual contents, I can > : read what a distributed system is doing with any old spoofer. > > So you could write your own 'Write or 'Output and > use text streams. Am I missing something? > > Georg Yes, read what I wrote before. My original concept was year ago way before Ada 95. Even with Ada 95, streams have additional overhead. And this does not address my original question of why are streams smart and image/value isn't. -- Samuel T. Harris, Principal Engineer Raytheon, Aerospace Engineering Services "If you can make it, We can fake it!" ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 16:19 ` Samuel T. Harris 2001-04-16 17:08 ` Marin David Condic @ 2001-04-16 17:37 ` Martin Dowie 1 sibling, 0 replies; 20+ messages in thread From: Martin Dowie @ 2001-04-16 17:37 UTC (permalink / raw) great minds... :-) I can see how the actual output may vary from compiler to compiler, but that sort of problem crops up all over the place. If the RM had put a few examples in it should have been _that_ hard? should it? Samuel T. Harris <u61783@gsde.hou.us.ray.com> wrote in message news:3ADB1BAA.6F4E0437@gsde.hou.us.ray.com... > I believe I asked that very same question here some > years ago. The response I got was the difficulty in > defining such a thing within the language and the > difficulties in implementing it in general. To which > I countered that the very same issues are relevant > to the definition and implementation of streams. > That is as far as the thread went (as far as my > imperfect memory recalls). ^ permalink raw reply [flat|nested] 20+ messages in thread
[parent not found: <0tEC6.7646$FY5.638172@www.newsranger.com>]
* Re: User define attributes [not found] ` <0tEC6.7646$FY5.638172@www.newsranger.com> @ 2001-04-16 17:34 ` Martin Dowie 0 siblings, 0 replies; 20+ messages in thread From: Martin Dowie @ 2001-04-16 17:34 UTC (permalink / raw) Is consistency such a big deal - if so, then it seems that it is streams that are being inconsistent... ;-) Ted Dennison <dennison@telepath.com> wrote in message news:0tEC6.7646$FY5.638172@www.newsranger.com... > That would be consistent with what is done with streams. But to enhance the > consistency, you'd want 'Image on aggregate to do a 'Image on each component, > and for that to work you'd want 'Image defined on *everything*. ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-16 14:35 ` Samuel T. Harris 2001-04-16 15:20 ` Martin Dowie @ 2001-04-18 12:14 ` Marius Amado Alves 2001-04-18 20:50 ` Samuel T. Harris 1 sibling, 1 reply; 20+ messages in thread From: Marius Amado Alves @ 2001-04-18 12:14 UTC (permalink / raw) To: comp.lang.ada On Mon, 16 Apr 2001, Samuel T. Harris wrote: > It is somewhat harder to build a generic for records > since the "index" are the field names and the composite > function depend upon which field is in use. But it > can be done. It can? You mean without passing the names of the components explicitly, parallelely? Please let us know how! I am really curious because some months ago this was briefly discussed here on CLA and the conclusion was negative. Not even a compiler dependent means was detected. -- Marius Amado Alves alves@systran.lu Project Tradaut-Pt Systran Luxembourg 12, Rue de Vianden L-2680 LUXEMBOURG Tel 352 + 45 46 53 Fax 352 + 45 74 75 ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-18 12:14 ` Marius Amado Alves @ 2001-04-18 20:50 ` Samuel T. Harris 2001-04-19 1:35 ` Jeffrey Carter 2001-04-20 19:08 ` Marius Amado Alves 0 siblings, 2 replies; 20+ messages in thread From: Samuel T. Harris @ 2001-04-18 20:50 UTC (permalink / raw) Marius Amado Alves wrote: > > On Mon, 16 Apr 2001, Samuel T. Harris wrote: > > It is somewhat harder to build a generic for records > > since the "index" are the field names and the composite > > function depend upon which field is in use. But it > > can be done. > > It can? You mean without passing the names of the components explicitly, > parallelely? Please let us know how! > > I am really curious because some months ago this was briefly discussed > here on CLA and the conclusion was negative. Not even a compiler > dependent means was detected. > Lets go back to the array. It needs generic formal parameters for the width, image, and value of its index type and its component type. An instantiation provides a width, image, and value function for the composite type (i.e. the array). This is easy because an array is a homogenous collection. No matter the value of the index, the handling of components are the same. Now, compare this to records. Records are a heterogenous collection. The handling of components depends upon which component one is dealing. So, the image, value, and width functions for the components must case off of the field name and defer to the appropriate field type. This case you have to write in preparation for you instantiation. Some code examples follow ... Here is the spec for a generic which supports arrays. generic type Index_Type is (<>); type Component_Type is private; type Array_Type is array (Index_Type range <>) of Component_Type; with function Width (Item : Index_Type) return Natural is <>; with function Image (Item : Index_Type) return String is <>; with function Value (Item : String) return Index_Type is <>; with function Width (Item : Component_Type) return Natural is <>; with function Image (Item : Component_Type) return String is <>; with function Value (Item : String) return Component_Type is <>; package Array_Attribute_Functions is -- The base type of the generic index_type must have a predecessor to -- index_type'first to support returning the value of an empty image "()". function Width (Item : Array_Type) return Natural; function Image (Item : Array_Type) return String; function Value (Item : String) return Array_Type; end Array_Attribute_Functions; And here is an instantiation ... type Test_Record_Array is array (Positive range <>) of Test_Record_Type; function Width (Item : Positive) return Natural; function Image (Item : Positive) return String; function Value (Item : String) return Positive; function Width (Item : Test_Record_Type) return Natural renames Test_Record_Type_Attributes.Width; function Image (Item : Test_Record_Type) return String renames Test_Record_Type_Attributes.Image; function Value (Item : String) return Test_Record_Type renames Test_Record_Type_Attributes.Value; package Test_Record_Array_Attributes is new Array_Attribute_Functions (Index_Type => Positive, Component_Type => Test_Record_Type, Array_Type => Test_Record_Array); ... where I could have used positive'image and positive'value for the index functions, but I cannot use positive'width since it is a parameterless function. I have to define a width function which is a simple one-liner. So, for consistency I also define an image and value function. Ignore, for the moment, the fact that with width, image, and value functions for the record type have not been defined yet, that will come later. I believe the implementation of the generic body is obvious to most everyone. Image simply iterates over the index of the parameter and calls the image of the index and the image of the component, puts a "=>" finger in between, adds a separating "," after each iteration, except the last. Finally it decorates the whole thing with enclosing "()". The necessary code to satisfy the generic instantiation is trivial for arrays. Now lets look at records ... generic type Field_Type is (<>); type Record_Type is private; -- Record fields should all have default values. with function Width (Item : Field_Type) return Natural is <>; with function Image (Item : Field_Type) return String is <>; with function Value (Item : String) return Field_Type is <>; -- Width of a field of the record with default value -- must represent the largest width possible. with function Width (Field : Field_Type; Item : Record_Type) return Natural is <>; with function Image (Field : Field_Type; Item : Record_Type) return String is <>; with procedure Value (Item : String; Field : Field_Type; Into : in out Record_Type) is <>; package Record_Attribute_Functions is function Width (Item : Record_Type) return Natural; function Image (Item : Record_Type) return String; function Value (Item : String) return Record_Type; end Record_Attribute_Functions; ... which look much like the one for arrays with the following differences. 1. Instead of an index type, we have a field type. 2. Width, Image, and Value functions dealing with the component have a field_type parameter. The corresponding array function did not since the array generic know how to index the array but the record generic cannot "index" the record by field. 3. Instead of a component value function, we have a value procedure which works on the composite type. The need for a value procedure is obvious when one considers that various fields may have various types and their is no way for a single value function to support those various types. So, instead of providing a value function, the coder must prepare a value procedure which incrementally fills in a working record kept by generic body. Here is an instantiation which provided the width, image, and value function for the array generic ... type Test_Record_Type is record I, J : Largest_Integer; X, Y, Z : Largest_Float; end record; type Test_Record_Field is (I, J, X, Y, Z); function Width (Item : Test_Record_Field) return Natural; function Image (Item : Test_Record_Field) return String; function Value (Item : String) return Test_Record_Field; function Width (Field : Test_Record_Field; Item : Test_Record_Type) return Natural; function Image (Field : Test_Record_Field; Item : Test_Record_Type) return String; procedure Value (Item : String; Field : Test_Record_Field; Into : in out Test_Record_Type); package Test_Record_Type_Attributes is new Record_Attribute_Functions (Test_Record_Field, Test_Record_Type); ... The body of width, image, and value which involve components must handle the "indexing" of field names to actual record components. This is most easily done with case statements where each field alternative calls the appropriate width, image, or value function according to the field types. Each field for image and value will have its own alternative. Normally the width will also have one alternative per field. As a side note for discriminent and variant records, the discriminants should come first. The value function cannot determine the actual type of the return value until it has all discriminants. This means all discriminant need to have default values to allow record variables to polymorph. The value function simply resets the in out parameter when it know it has all the other discriminants. All three subprograms; width, image, and value; may employ sophisticated logic to handle variant records in order to avoid access invalid fields as they interate over the "index" field_type. Or they can just simply make the attempt and handle constraint_error with a very localized null exception handler. I hope that answers your questions. Since this code was written for a particular project, I am not free to release the contents. However, if there is some demand, I shall rewrite it from scratch at home and submit it to www.adapower.com. -- Samuel T. Harris, Principal Engineer Raytheon, Aerospace Engineering Services "If you can make it, We can fake it!" ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-18 20:50 ` Samuel T. Harris @ 2001-04-19 1:35 ` Jeffrey Carter 2001-04-19 12:50 ` Samuel T. Harris 2001-04-20 19:08 ` Marius Amado Alves 1 sibling, 1 reply; 20+ messages in thread From: Jeffrey Carter @ 2001-04-19 1:35 UTC (permalink / raw) "Samuel T. Harris" wrote: > > generic > type Index_Type is (<>); > type Component_Type is private; > type Array_Type is array (Index_Type range <>) of Component_Type; > with function Width (Item : Index_Type) return Natural is <>; > with function Image (Item : Index_Type) return String is <>; > with function Value (Item : String) return Index_Type is <>; Note that since Index_Type is a discrete type, the generic can call Index_Type'Image, Index_Type'Value, and so on directly. -- Jeff Carter "You couldn't catch clap in a brothel, silly English K...niggets." Monty Python & the Holy Grail ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-19 1:35 ` Jeffrey Carter @ 2001-04-19 12:50 ` Samuel T. Harris 0 siblings, 0 replies; 20+ messages in thread From: Samuel T. Harris @ 2001-04-19 12:50 UTC (permalink / raw) Jeffrey Carter wrote: > > "Samuel T. Harris" wrote: > > > > generic > > type Index_Type is (<>); > > type Component_Type is private; > > type Array_Type is array (Index_Type range <>) of Component_Type; > > with function Width (Item : Index_Type) return Natural is <>; > > with function Image (Item : Index_Type) return String is <>; > > with function Value (Item : String) return Index_Type is <>; > > Note that since Index_Type is a discrete type, the generic can call > Index_Type'Image, Index_Type'Value, and so on directly. > Reread my post. I address that issue. Width cannot be assumed, due to the other kinds of generics I have written (like address_attribute_functions) so it must be "functionized" in certain places, so I functionize it in all places. Similarly for image and value. It is there for the sake on consistency. Besides, I may want to instantiate this generic with something other than the attribute functions or their equivalent. I might want some particular format, even for integers or floats (like say using base 16 instead of base 10). My general rule on generics is pass in all functionality and default then to "is <>". This includes numerics. Generics involving numeric types should pass in all the operators. This notion is especially important when one considers that limited private types with a new equality operator defined will revert back to the default equality in generic instantiations. Better to pass that "=" in. Since these are usually defaulted for me, I don't both providing in the generic instantiation. I only have to bother with ensuring they are visible with appropriate use clauses. -- Samuel T. Harris, Principal Engineer Raytheon, Aerospace Engineering Services "If you can make it, We can fake it!" ^ permalink raw reply [flat|nested] 20+ messages in thread
* Re: User define attributes 2001-04-18 20:50 ` Samuel T. Harris 2001-04-19 1:35 ` Jeffrey Carter @ 2001-04-20 19:08 ` Marius Amado Alves 1 sibling, 0 replies; 20+ messages in thread From: Marius Amado Alves @ 2001-04-20 19:08 UTC (permalink / raw) To: comp.lang.ada On Wed, 18 Apr 2001, Samuel T. Harris wrote: > > generic > type Index_Type is (<>); > type Component_Type is private; > ... > package Array_Attribute_Functions is > function Width (Item : Array_Type) return Natural; > function Image (Item : Array_Type) return String; > function Value (Item : String) return Array_Type; > end Array_Attribute_Functions; > > ... My package Bundles is along those lines. generic type Names is (<>); package Bundles is type Bundle_Type is array(Names) of Unbounded_String; function Bundle( S: String; Indicator: String := "=>"; Separator: String := ",") return Bundle_Type; -- Parses a bundle representation S into a Bundle_Type. -- White space between tokens is ignored. -- Unnamed items are assigned Null_Unbounded_String. -- B = Bundle(Image(B)) holds. function Image( ... It has served me long and well. It is now in Adlib (lexis.di.fct.unl.pt/ADaLIB), category Miscelany. -- Marius Amado Alves alves@systran.lu Project Tradaut-Pt Systran Luxembourg 12, Rue de Vianden L-2680 LUXEMBOURG Tel 352 + 45 46 53 Fax 352 + 45 74 75 Mob 351 +939354002 ^ permalink raw reply [flat|nested] 20+ messages in thread
end of thread, other threads:[~2001-04-24 21:00 UTC | newest] Thread overview: 20+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2001-04-14 13:33 User define attributes Ayende Rahien 2001-04-16 7:27 ` Martin Dowie 2001-04-16 11:23 ` Ayende Rahien 2001-04-16 11:37 ` Martin Dowie 2001-04-16 14:35 ` Samuel T. Harris 2001-04-16 15:20 ` Martin Dowie 2001-04-16 15:37 ` Brian Rogoff 2001-04-16 16:19 ` Samuel T. Harris 2001-04-16 17:08 ` Marin David Condic 2001-04-16 22:00 ` Samuel T. Harris 2001-04-17 12:34 ` Georg Bauhaus 2001-04-17 20:35 ` Samuel T. Harris 2001-04-24 21:00 ` Samuel T. Harris 2001-04-16 17:37 ` Martin Dowie [not found] ` <0tEC6.7646$FY5.638172@www.newsranger.com> 2001-04-16 17:34 ` Martin Dowie 2001-04-18 12:14 ` Marius Amado Alves 2001-04-18 20:50 ` Samuel T. Harris 2001-04-19 1:35 ` Jeffrey Carter 2001-04-19 12:50 ` Samuel T. Harris 2001-04-20 19:08 ` Marius Amado Alves
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox