comp.lang.ada
 help / color / mirror / Atom feed
* Using interfaces
@ 2018-06-02 15:20 gautier_niouzes
  2018-06-02 15:55 ` Jere
                   ` (3 more replies)
  0 siblings, 4 replies; 9+ messages in thread
From: gautier_niouzes @ 2018-06-02 15:20 UTC (permalink / raw)


Hello,
I am developing a package for displaying some kinds of figures on an open set of devices. I'm trying to use interfaces for that. 

with System;

package Test_interfaces is

  type Real is digits System.Max_Digits;

  type Box is record left, bottom, width, height : Real; end record;

  type Graphic_Figures is abstract tagged record bounding : Box; end record;
  
  procedure Set_Bounding_Box (figure: Graphic_Figures);
  --  This draws any kind of figure
  procedure Draw (figure: Graphic_Figures; parameter: Integer) is abstract;

  type Any_Device is interface;
  --  Callback method for filling a rectangle
  procedure Filled_Rectangle (device: Any_Device) is abstract;

  type Device_X is new Any_Device with null record;
  procedure Filled_Rectangle (device: Device_X);

  type Figure_A is new Graphic_Figures with null record;
  procedure Draw (figure: Figure_A; parameter: Integer);

  type Figure_A_on_Device_X is new Figure_A and Device_X with null record;

end Test_interfaces;

Now on Figure_A_on_Device_X GNAT tells me rightfully that Device_X must be an interface. Now if I have instead:

  type Figure_A_on_Device_X is new Figure_A and Any_Device with null record;
  procedure Filled_Rectangle (device: Figure_A_on_Device_X);

or in two steps,

  type Figure_A_on_Any_Device is abstract new Figure_A and Any_Device with null record;

  type Figure_A_on_Device_X is new Figure_A_on_Any_Device with null record;
  procedure Filled_Rectangle (device: Figure_A_on_Device_X);

this is okay but I would need to copy-paste the same implementation code of Filled_Rectangle for device X in the case of Figure_B and so on. Not nice!

How do I get something like Figure_A_on_Device_X ?
Am I expecting too much from interfaces?

TIA

Gautier
_____________________________________________________________
A free online game in Ada: http://pasta.phyrama.com/game.html

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

* Re: Using interfaces
  2018-06-02 15:20 Using interfaces gautier_niouzes
@ 2018-06-02 15:55 ` Jere
  2018-06-02 17:00 ` Dmitry A. Kazakov
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 9+ messages in thread
From: Jere @ 2018-06-02 15:55 UTC (permalink / raw)


On Saturday, June 2, 2018 at 11:20:53 AM UTC-4, gautier...@hotmail.com wrote:
> Hello,
> I am developing a package for displaying some kinds of figures on an open set of devices. I'm trying to use interfaces for that. 
> 
> SNIPPED...
>
> this is okay but I would need to copy-paste the same implementation code of Filled_Rectangle for device X in the case of Figure_B and so on. Not nice!
> 
> How do I get something like Figure_A_on_Device_X ?
> Am I expecting too much from interfaces?
> 

I generally do something similar to what you do in Rust:

      type Figure_A_on_Device_X is new Figure_A and Any_Device with record
         Device_Impl : Device_X;
      end record;
      procedure Filled_Rectangle (device: Figure_A_on_Device_X);

and then

      procedure Filled_Rectangle (device: Figure_A_on_Device_X) is
      begin
         device.Device_Impl.Filled_Rectangle;
      end Filled_Rectangle;

You can also make Figure_A_on_Device_X a private type to hide that.

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

* Re: Using interfaces
  2018-06-02 15:20 Using interfaces gautier_niouzes
  2018-06-02 15:55 ` Jere
@ 2018-06-02 17:00 ` Dmitry A. Kazakov
  2018-06-02 17:22 ` Jeffrey R. Carter
  2018-06-04  9:32 ` gautier_niouzes
  3 siblings, 0 replies; 9+ messages in thread
From: Dmitry A. Kazakov @ 2018-06-02 17:00 UTC (permalink / raw)


On 2018-06-02 17:20, gautier_niouzes@hotmail.com wrote:

> I am developing a package for displaying some kinds of figures on an open set of devices. I'm trying to use interfaces for that.

This is full multiple dispatch [*]:

    procedure Draw
              (  Shape   : Figure;
                 Surface : in out Device
              )  is abstract;            -- This is not Ada!

Interfaces is no [direct] help.

Usually multiple dispatch is replaced with cascaded dispatch. You first 
dispatch on the figure and then on the device from the implementation of 
Draw:

    type Device is limited interface;
    -- Drawing primitives
    procedure Draw_Filled_Rectangle
              (  Surface : in out Device;
                 Box     : Rectangle;
                 Color   : RGBA_Color
              )  is abstract;
    ...

    type Figure is interface;
    procedure Draw
              (  Shape   : Figure;
                 Surface : in out Device'Class -- No dispatch
              )  is abstract;


    type Filled_Rectangle is new Figure with record
       Position : Box;
       Color    : RGBA_Color;
    end record;
    overriding
       procedure Draw
                 (  Shape   : Filled_Rectangle;
                    Surface : in out Device'Class
                 );

    procedure Draw
              (  Shape   : Filled_Rectangle;
                 Surface : in out Device'Class
              )  is
    begin
       Surface.Draw_Filled_Rectangle (Shape.Position, Shape.Color);
    end Draw;

This is too simple to work always in practice. There is no replacement 
for multiple dispatch.

-------------------------------
* A simplified form of multiple dispatch is multi-method when only one 
types hierarchy is involved, like in arithmetic operations. Full 
multiple dispatch is when different type hierarchies share operations 
like Figure and Device share Draw.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


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

* Re: Using interfaces
  2018-06-02 15:20 Using interfaces gautier_niouzes
  2018-06-02 15:55 ` Jere
  2018-06-02 17:00 ` Dmitry A. Kazakov
@ 2018-06-02 17:22 ` Jeffrey R. Carter
  2018-06-02 18:43   ` Dan'l Miller
  2018-06-04  9:32 ` gautier_niouzes
  3 siblings, 1 reply; 9+ messages in thread
From: Jeffrey R. Carter @ 2018-06-02 17:22 UTC (permalink / raw)


On 06/02/2018 05:20 PM, gautier_niouzes@hotmail.com wrote:
> Hello,
> I am developing a package for displaying some kinds of figures on an open set of devices. I'm trying to use interfaces for that.

"IMHO, Interfaces are worthless."
Randy Brukardt

He usually knows what he's talking about.

-- 
Jeff Carter
"C++ is like giving an AK-47 to a monk, shooting him
full of crack and letting him loose in a mall and
expecting him to balance your checking account
'when he has the time.'"
Drew Olbrich
52


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

* Re: Using interfaces
  2018-06-02 17:22 ` Jeffrey R. Carter
@ 2018-06-02 18:43   ` Dan'l Miller
  2018-06-03  2:11     ` Shark8
  0 siblings, 1 reply; 9+ messages in thread
From: Dan'l Miller @ 2018-06-02 18:43 UTC (permalink / raw)


On Saturday, June 2, 2018 at 12:22:09 PM UTC-5, Jeffrey R. Carter wrote:
> On 06/02/2018 05:20 PM, Gautier Niouzes wrote:
> > Hello,
> > I am developing a package for displaying some kinds of figures on an open set of devices. I'm trying to
> > use interfaces for that.

If you are yearning for multiple dispatch, then it could conceivably be achieved in modern Ada the way that single-dispatch OO was described for Ada83:

http://home.pipeline.com/~hbaker1/OOAdaLetters.html

Or you could hack multimethods into a forked GNAT.  The primary annoyance is how best to achieve a multidimensional look-up based on a coordinate of 2 or more tags, e.g., in O(1) time and O(mnᵏ) space(-per-executable/DLL) would be the multidimensional-array implementation (instead of today's single-dimensional tag-based array), where m is the quantity of tagged records, n is the quantity of densely-type-dispatched multimethods per tagged record, and k is the degree of mult- in multimethods/multiple-dispatch.  k=2 is called double dispatch.  k=3 is called triple dispatch. and so forth

A secondary annoyance is erroring out at compile-time if the program has any sparseness whatsoever in that multidimensional array (i.e., forgot to declare a multimethod for some combination of 'Classes).

> "IMHO, Interfaces are worthless."
> Randy Brukardt
> 
> He usually knows what he's talking about.

Ummmmm.  I suspect that Randy's statement neither started nor ended there.  Instead of being in ‘a pox on all their houses’ and ‘the horse they rode in on’ mode, I suspect that Randy in the very next sentence or paragraph said that language-construct X instead better accomplishes what ‘interface’-keyword constructs seek to do.

For example, in many modern programming languages, Ada included, there is a natural tension between the OO feature-set and the generic/parameterized-types feature-set as 2 nearly duplicated ways of accomplishing analogous goals.  Of course this is because runtime/tagged-record polymorphism and parametric/generic polymorphism are 2 forms of polymorphism at different vertexes on the lambda cube.

https://en.wikipedia.org/wiki/Lambda_cube

> "C++ is like giving an AK-47 to a monk, shooting him 
> full of crack and letting him loose in a mall and 
> expecting him to balance your checking account 
> 'when he has the time.'"

I would say that modern C++ is like giving the template engine Turing completeness with a syntax that is only slightly more readable than a Turing-machine's infinite-length tape and then making the entire reason for the language to exist to constantly put bandages on the never-quite-healing wounds inflicted by the Turing-completeness-in-the-template-engine bull goring the rest of the C++ language with its horns, as if the rest of the language is a porcelain shop that stocks only red items.  Oh wait.  No “like” about it; that is what really happened.


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

* Re: Using interfaces
  2018-06-02 18:43   ` Dan'l Miller
@ 2018-06-03  2:11     ` Shark8
  2018-06-03  3:06       ` Dan'l Miller
  2018-06-03  7:06       ` Dmitry A. Kazakov
  0 siblings, 2 replies; 9+ messages in thread
From: Shark8 @ 2018-06-03  2:11 UTC (permalink / raw)


On Saturday, June 2, 2018 at 12:43:34 PM UTC-6, Dan'l Miller wrote:
> 
> For example, in many modern programming languages, Ada included, there is a natural tension between the OO feature-set and the generic/parameterized-types feature-set as 2 nearly duplicated ways of accomplishing analogous goals.

No.
OOP and Generics are orthogonal in  their goals: https://www.youtube.com/watch?v=9lv2lBq6x4A

The "tension" is due to a fundamental misunderstanding of what OOP *is*, which I think is due to the whole OOP-craze in academia, to the point where other ideas were forgotten/discarded: https://www.youtube.com/watch?v=8pTEmbeENF4&nohtml5=1


> I would say that modern C++ is like giving the template engine Turing completeness with a syntax that is only slightly more readable than a Turing-machine's infinite-length tape and then making the entire reason for the language to exist to constantly put bandages on the never-quite-healing wounds inflicted by the Turing-completeness-in-the-template-engine bull goring the rest of the C++ language with its horns, as if the rest of the language is a porcelain shop that stocks only red items.  Oh wait.  No “like” about it; that is what really happened.

The amount of time/energy spent "fixing C" (and C++) is truly astounding.
I wonder how well-off Ada would be if even 1/10th of that energy had been put into addressing Ada's weaknesses, like (eg) few libraries, not as many tools, etc.


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

* Re: Using interfaces
  2018-06-03  2:11     ` Shark8
@ 2018-06-03  3:06       ` Dan'l Miller
  2018-06-03  7:06       ` Dmitry A. Kazakov
  1 sibling, 0 replies; 9+ messages in thread
From: Dan'l Miller @ 2018-06-03  3:06 UTC (permalink / raw)


On Saturday, June 2, 2018 at 9:11:50 PM UTC-5, Shark8 wrote:
> On Saturday, June 2, 2018 at 12:43:34 PM UTC-6, Dan'l Miller wrote:
> > 
> > For example, in many modern programming languages, Ada included, there is a natural tension between the OO feature-set and the generic/parameterized-types feature-set as 2 nearly duplicated ways of accomplishing analogous goals.
> 
> No.
> OOP and Generics are orthogonal in  their goals: https://www.youtube.com/watch?v=9lv2lBq6x4A

Orthogonal.  Hence the lambda cube's 90° angles in the corners. ;-P

https://en.wikipedia.org/wiki/Lambda_cube

the orthogonal axes in the coordinate system

run-time •polymorphism•:  values depending on types
λ2

compile-time •polymorphism• (a.k.a. parametric •polymorphism•) in generics:  types depending on types
λω_

multistage programming (source-code generators):  types depending on values  
λΠ

permitting the pairwise and threesome combination thereof for a semantic vector system of expressivity in next-gen programming languages

> The amount of time/energy spent "fixing C" (and C++) is truly astounding.
> I wonder how well-off Ada would be if even 1/10th of that energy had been put into addressing Ada's
> weaknesses, like (eg) few libraries, not as many tools, etc.

It is truly is sad.

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

* Re: Using interfaces
  2018-06-03  2:11     ` Shark8
  2018-06-03  3:06       ` Dan'l Miller
@ 2018-06-03  7:06       ` Dmitry A. Kazakov
  1 sibling, 0 replies; 9+ messages in thread
From: Dmitry A. Kazakov @ 2018-06-03  7:06 UTC (permalink / raw)


On 2018-06-03 04:11, Shark8 wrote:
> On Saturday, June 2, 2018 at 12:43:34 PM UTC-6, Dan'l Miller wrote:
>>
>> For example, in many modern programming languages, Ada included, there is a natural tension between the OO feature-set and the generic/parameterized-types feature-set as 2 nearly duplicated ways of accomplishing analogous goals.
> 
> No.

Yes. Both are forms of polymorphism with the same goal of supporting 
generic programming = programming in terms of sets of types.

> OOP and Generics are orthogonal in  their goals: https://www.youtube.com/watch?v=9lv2lBq6x4A

They are orthogonal only in terms of implementation, so that they can be 
mixed and used independently.

[ Macros are automatically a meta-language and thus orthogonal to 
anything in the language. ]

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de


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

* Re: Using interfaces
  2018-06-02 15:20 Using interfaces gautier_niouzes
                   ` (2 preceding siblings ...)
  2018-06-02 17:22 ` Jeffrey R. Carter
@ 2018-06-04  9:32 ` gautier_niouzes
  3 siblings, 0 replies; 9+ messages in thread
From: gautier_niouzes @ 2018-06-04  9:32 UTC (permalink / raw)


Thanks for all those interesting answers!
Finally I've abstracted only the output device and things stay quite simple.
(for a glimpse: https://sourceforge.net/projects/ada-bar-codes/ :-) )

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

end of thread, other threads:[~2018-06-04  9:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-06-02 15:20 Using interfaces gautier_niouzes
2018-06-02 15:55 ` Jere
2018-06-02 17:00 ` Dmitry A. Kazakov
2018-06-02 17:22 ` Jeffrey R. Carter
2018-06-02 18:43   ` Dan'l Miller
2018-06-03  2:11     ` Shark8
2018-06-03  3:06       ` Dan'l Miller
2018-06-03  7:06       ` Dmitry A. Kazakov
2018-06-04  9:32 ` gautier_niouzes

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