From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.5-pre1 (2020-06-20) on ip-172-31-74-118.ec2.internal X-Spam-Level: * X-Spam-Status: No, score=1.8 required=3.0 tests=BAYES_50,FREEMAIL_FROM, PDS_FROM_2_EMAILS autolearn=no autolearn_force=no version=3.4.5-pre1 X-Received: by 2002:aed:3ac5:: with SMTP id o63mr7636701qte.239.1611712084349; Tue, 26 Jan 2021 17:48:04 -0800 (PST) X-Received: by 2002:a25:6ec3:: with SMTP id j186mr12945619ybc.165.1611712083767; Tue, 26 Jan 2021 17:48:03 -0800 (PST) Path: eternal-september.org!reader02.eternal-september.org!news.mixmin.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Tue, 26 Jan 2021 17:48:03 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=199.204.203.151; posting-account=XGCYegoAAADY19DGgU_zTfTSbVlfUJ_a NNTP-Posting-Host: 199.204.203.151 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <546f7f19-cdda-4eb4-9202-9361905b8726n@googlegroups.com> Subject: Re: Simple example on interfaces From: "philip...@gmail.com" Injection-Date: Wed, 27 Jan 2021 01:48:04 +0000 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Xref: reader02.eternal-september.org comp.lang.ada:61215 List-Id: > "IMHO, Interfaces are worthless."=20 I find interfaces to be extremely valuable for abstracting I/O devices. Fo= r example in my Linux Simple I/O Library, there is code equivalent to the f= ollowing (the actual code is different, as I sucked a lot of common boilerp= late for I/O device interfaces into a generic package that is instantiated = for each data item type): package GPIO is type Direction is (Input, Output); type PinInterface is interface; type Pin is access all PinInterface'Class; procedure Put(Self : PinInterface; state : Boolean); function Get(Self : PinInterface) return Boolean; end GPIO; I've probably defined a dozen packages that implement GPIO pins using every= thing from Linux kernel services to web servers. Every one of them contain= s a function like this: function Create(...) return GPIO.Pin; This allows code like the following: GPIO1 : GPIO.Pin :=3D GPIO.libsimpleio.Create(RaspberryPi.GPIO18, GPIO.Ou= tput); GPIO2 : GPIO.Pin :=3D GPIO.HTTP.Create("http://foo.munts.net", 5, GPIO.Ou= tput); GPIO3 : GPIO.Pin :=3D GPIO.RemoteIO.Create(server, 7, GPIO.Output); This allows GPIO pins scattered far and near throughout the known universe = to be treated exactly the same, even collected into an array or container. I very seldom implement more than one interface in a type definition though= , unless a single device has multiple sensors (temperature and humidity, fo= r instance). Microsoft's .Net uses this scheme pervasively, though I originally learned = it in Ada and later applied the same thinking to .Net, Free Pascal, Java, P= ython, and C++ (and other languages).