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 X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,UTF8 Received: by 10.180.84.234 with SMTP id c10mr1680918wiz.4.1351478331948; Sun, 28 Oct 2012 19:38:51 -0700 (PDT) Path: ha8ni72146wib.1!nntp.google.com!feeder2.cambriumusenet.nl!feed.tweaknews.nl!81.171.88.15.MISMATCH!lightspeed.eweka.nl!81.171.88.16.MISMATCH!eweka.nl!hq-usenetpeers.eweka.nl!border3.nntp.ams.giganews.com!border1.nntp.ams.giganews.com!border4.nntp.ams.giganews.com!border2.nntp.ams.giganews.com!nntp.giganews.com!newsreader4.netcologne.de!news.netcologne.de!newsfeed.straub-nv.de!news.mb-net.net!open-news-network.org!aioe.org!.POSTED!not-for-mail From: =?utf-8?Q?Yannick_Duch=C3=AAne_=28Hibou57?= =?utf-8?Q?=29?= Newsgroups: comp.lang.ada Subject: Re: Conditional holder for a type with unknown discriminant Date: Sat, 27 Oct 2012 14:03:44 +0200 Organization: Ada @ Home Message-ID: References: <6ddd255c-01e6-465d-8f42-cc3a5014a8ba@googlegroups.com> NNTP-Posting-Host: 3BwS2FG+75pYINFjPK6b7w.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-27T14:03:44+02:00 List-Id: Le Sat, 27 Oct 2012 11:15:30 +0200, AdaMagica = a =C3=A9crit: > A.18.18 Indefinite Holders might help. I had a look back at it, after your suggestion, but that will not do the= = trick (except for one thing). Let me explain what I'm looking for with the record construct explain in= = the initial post. First, an example in Ada (again), then, a similar thin= g = in SML, with comments to compare both. type S is (Is_Null, Has_Item); type R (D : S) is record case S is when Is_Null =3D> null; when Has_Item =3D> I : T; end case; end record; which could be used this way: Y : R :=3D F (...); case R.S is when Is_Null =3D> -- Do nothing which could -- attempt to access `R.I`. null; when Has_Item =3D> P (R.I); -- Do whatever you want -- on `R.I`. end case; Now, the SML way, which looks very close, but which I can reach only = imperfectly in Ada (see comments below): datatype R =3D Is_Null | Has_Item of T; which could be used this way: let y =3D f (...) in case y of Is_Null =3D> -- What you want, -- but no access to I. Has_Item (i) =3D> -- What you want of I. end; Now the comparison is that in the Ada case, you can still attempt to = access `R.I` in the `Is_Null` branch=E2=80=A6 you will obviously get an = error at = run=E2=80=91time, but it will compile (may be with compiler warnings, bu= t it will = compile). In the SML case, you just have simply no way to erroneously = access `i`, that impossible, because unless SML matched the structure `y= ` = to the pattern `Has_Item (y)`, then `y` simply does not exist and is not= = accessible at all. Anyway, I still wanted to do the best with Ada to mat= ch = this construct, and this works, except when the type of `I` in this = example, has an unknown discriminant. But I think there is an other option, more close to the SML semantic, = while less close semantically, which is to use a call=E2=80=91back subpr= ogram to = be applied on `I`. That way, you the same as with SML: unless `I` was = really passed to you, you have strictly no way to access it, that's = absolute safety. Has a side note, this may be an argument for active iterators over passi= ve = iterators. But that does not work well for all cases. That's OK as long = as = the operations to be applied on each iterated item is the same and, more= = importantly, always occurs in the same context, otherwise, things become= = complicated to setup, and the passive iterators looks better in such = cases; but with the drawback it has. Has another side note, the way SML do things (which work well) for reque= st = function/iterator, may suggest the test should be done on the return = result, instead of on the iterator. I also have some though about compared efficiency of the call=E2=80=91ba= ck method = with an active iterator vs the passive iterator, but I'm not sure it's = worth to expose. Well, so far, I think I will go for passive with call back for iterators= = and request functions. Will see if that still looks OK after some time = doing things with it. -- = =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