From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,9eaef25f8adbf762 X-Google-Attributes: gid103376,public From: stt@henning.camb.inmet.com (Tucker Taft) Subject: Re: access to subprogram disciminants Date: 1996/04/25 Message-ID: #1/1 X-Deja-AN: 151366337 sender: news@inmet.camb.inmet.com (USENET news) x-nntp-posting-host: henning.camb.inmet.com references: <4lm5t3$bqb@news2.delphi.com> organization: Intermetrics, Inc. newsgroups: comp.lang.ada Date: 1996-04-25T00:00:00+00:00 List-Id: tmoran@bix.com wrote: : ... : >: 2) Other than generics or discriminants of records (or tasks), : >: what other ways are there of creating parameterized objects in Ada? : > : >Again, I am not exactly sure what you mean by "parameterized" : >objects. It might be helpful to understand the ultimate problem : I have existing Ada 83 Windows code using generics in the form: : procedure main_window_procedure(Message, ... : package main_window is new Windows.frame_windows( : width =>200, : height =>200, : title =>"Cars and Drivers", : wndproc=>main_window_procedure); : car_list:main_window.listboxes; : driver_name_dialog:main_window.dialog_boxes; : begin : which_car:=main_window.open(car_list); : etc. : I think of 'main_window' as an object, parameterized by width, height, : title, wndproc, etc. Though generics have several nice characteristics, : I can't easily make an array of them, for instance, so, now that : 'access to subprogram' and 'access all' are available in Ada 95, it may : be reasonable to have instead : type frame_windows(width, height: integer; : title: string_ptr; : wndproc: wndproc_ptr) is ... : main_window:aliased frame_windows( : width =>200, : height =>200, : title =>title_string_ptr, : wndproc=>main_window_procedure'access); : car_list:Windows.listboxes(main_window'access); : driver_name_dialog:Windows.dialog_boxes(main_window'access); : begin : which_car:=Windows.open(car_list); : (Pardon typos and elisions. This is typed for posting and is intended : to show the idea, not to be running code.) : That is what I mean by creating a parameterized object. Unless I'm : missing something, or some clever way of using record tags, Ada 95 : provides generics, records with discriminants, and tasks with : discriminants, as ways of declaring such things. (Discounting : highly limited things like the size of an array as a 'parameter' : of the array) I think you are perhaps being misled by the RM's use of the term "parameterized" in association with type discriminants. What you are trying to accomplish can be done using normal record *components*, initialized as a result of calling a function or procedure. In other OOP's, such a function would often be called a "constructor," but in Ada, you can just use normal functions or procedures as "constructors." Don't try to make all of these "parameters" into discriminants. Just declare them as components of the data type. Then, a user would declare a simple "Frame_Window" or whatever, and initialize it from the result of a function call, or by a subsequent procedure call. The parameters to this initializing function/procedure are the "parameters" you are probably interested in. This approach is much more flexible, as you can have multiple constructor subprograms, taking different combinations of parameters. If you are doing a GUI, chances are that you will be using access types for representing these GUI elements, so it might be more natural and efficient to make your constructor "functions" return an access value, rather than a record object. An additional advantage is that you can make the entire type private, and choose to represent the information in various ways. If you make all the "parameters" into discriminants, then they are always visible to all clients, and you are forced to represent them explicitly, rather than perhaps implicitly in some other part of the data type. -Tucker Taft stt@inmet.com http://www.inmet.com/~stt/ Intermetrics, Inc. Cambridge, MA USA