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=-0.6 required=5.0 tests=BAYES_00,DATE_IN_PAST_24_48, FREEMAIL_FROM autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,69b3e9746c62ec93,start X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,UTF8 Received: by 10.180.94.98 with SMTP id db2mr1675862wib.2.1351478331933; Sun, 28 Oct 2012 19:38:51 -0700 (PDT) Path: q13ni92136wii.0!nntp.google.com!feeder1.cambriumusenet.nl!feeder3.cambriumusenet.nl!feed.tweaknews.nl!81.171.88.16.MISMATCH!eweka.nl!hq-usenetpeers.eweka.nl!news.astraweb.com!border5.a.newsrouter.astraweb.com!border2.nntp.ams.giganews.com!nntp.giganews.com!newsreader4.netcologne.de!news.netcologne.de!newsfeed.straub-nv.de!news2.arglkargh.de!news.mixmin.net!aioe.org!.POSTED!not-for-mail From: =?utf-8?Q?Yannick_Duch=C3=AAne_=28Hibou57?= =?utf-8?Q?=29?= Newsgroups: comp.lang.ada Subject: Conditional holder for a type with unknown discriminant Date: Sat, 27 Oct 2012 04:50:52 +0200 Organization: Ada @ Home Message-ID: NNTP-Posting-Host: q6HlfgUjnjacTi52J0+pXw.user.speranza.aioe.org Mime-Version: 1.0 X-Complaints-To: abuse@aioe.org User-Agent: Opera Mail/12.02 (Linux) X-Notice: Filtered by postfilter v. 0.8.2 Content-Type: text/plain; charset=utf-8; format=flowed; delsp=yes Content-Transfer-Encoding: Quoted-Printable Date: 2012-10-27T04:50:52+02:00 List-Id: Hi Ada hackers (lol, does this word can comes Ada?), Not the first time I face this question, just that it came back to me. The matter: a conditional holder =C3=A0=E2=80=91la SML is an idiom easil= y expressed in = Ada, except when the object to be wrapped also has to have an unknown = discriminant. Example: type E_Type is private; type W_Type (Is_Null : Boolean) is record case Is_Null is when True =3D> null; when False =3D> E : E_Type; end case; end record; function F return W_Type; `W_Type` is the result of a request returning an element of type `E_Type= `, = which may return nothing. Pros: avoid testing of condition dissociated from the result. Cons: troubles when `E_Type` has unknown discriminant. type E_Type (<>) is private; type W_Type (Is_Null : Boolean) is record case Is_Null is when True =3D> null; when False =3D> E : E_Type; end case; end record; function F return W_Type; Oops, unconstrained subtype in record definition. You may give default values to discriminant, but no there may be no well= = good enough defaults, and you may also want the client side to not creat= e = an instance of `E_Type`, which would not be meaningful. I tried to have this: type E_Type (<>) is private; type W_Type (Is_Null : Boolean) is tagged record case Is_Null is when True =3D> null; when False =3D> E : not null access E_Type; end case; end record; function F return W_Type'Class; Then, a type derived from `W_Type` extends it with an added `E_Type` = aliased member=E2=80=A6 I bet you guess I would like to be able to assig= n `E` a = reference to that member, but that's a no way: compiling the specificati= on = is OK, but compiling any implementation fails. Should I give up with this and use a precondition instead? What a pity we can't have a structure referencing it's own component. Th= at = would be perfectly valid from an access point of view, but we can't use = a = declaration before it's terminated, so access discriminant does not help= = here, as what is required, is an access to an inner element: X : X_Type (... E_Object =3D> (...), E =3D> X.E_Object'Access); -- Fails It happens I miss we could do that (providing `X_Type` would be a limite= d = type, of course). -- = =E2=80=9CSyntactic sugar causes cancer of the semi-colons.=E2=80=9D [1] =E2=80=9CStructured Programming supports the law of the excluded muddle.= =E2=80=9D [1] [1]: Epigrams on Programming =E2=80=94 Alan J. =E2=80=94 P. Yale Univers= ity