cho@umn-cs.UUCP (06/26/85)
/* Written 4:05 pm Jun 85, 19783 by harvard!macrakis in umn-cs:net.micro */ /* ---------- "Unchecked conversion in Ada" ---------- */ More Ada misinformation. This is getting dull. In my article <192@harvard.ARPA>, I wrote: > > ...unchecked_conversion, is so simple to implement (you just turn > > off type checking!).... This is the main thing you need to do > > C-like low-level pointer manipulations. Darin Johnson replies: <266@sdcc13> ...say C programmers start to use > Ada.... [what] if they ... [p]ut unchecked_conversion at the start > of all their programs. Then when they wanted to make distributions, > they would remove it.... [and eventually] suggest that untyped_ > conversion should be the default... This is not how Unchecked_conversion works. It is a generic function which can be instantiated to produce a function which converts between any two types bit-to-bit; something like C's casts. So there is no danger of global unchecked conversions. See Appendix for an example (or study Ada before talking about it!). Thus, although it is in principle possible to commit the usual C uglinesses in Ada, the language reminds you (...!) whenever you're slipping into low-think. I was simply refuting the argument `you can't do xxx in Ada', not saying that you ought to do xxx in Ada! -s Appendix How to use unchecked_conversion in Ada -- Suppose the type of the records in a file is determined in the -- file itself. Then you cannot expect to use the standard Ada -- sequential I/O package. Instead, you read into a binary buffer -- and use unchecked_conversion. type rec is ...; subtype bufslice is ...; ... function Buf_to_rec(words: bufslice) return rec is new Unchecked_conversion(bufslice,rec); -- no code generated ... thisrec := Buf_to_rec(Buffer(i..i+rec'size-1)); -- assuming Buffer is an array of storage-units. Alternatively, you can use pointers: ... type recptr is access rec; ... function Bufaddr_to_recptr(adr: address) return recptr is new Unchecked_conversion(address,recptr); -- no code generated ... thisrecptr := Bufaddr_to_recptr(Buffer(i)'address); -- no code generated for conversion nor 'address /* End of text from umn-cs:net.micro */