Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This situation is precisely what the attribute <code>'input</code> is in the language for. </p> <p>If you also own the code that writes that data out to the stream in the first place, then this is easy. Just use </p> <pre><code>Myobject : Discriminated_Record := Discriminated_Record'input (Some_Stream'access); </code></pre> <p>(and of course use <code>'output</code> when writing).</p> <p>If you must instead read in someone else's formatted data, it gets a wee bit more complicated. You will have to implement your own <code>'input</code> routine.</p> <pre><code>function Discriminated_Record_Input (Stream : access Ada.Streams.Root_Stream_Type'class) return Discriminated_Record; for Discriminated_Record'input use Discriminated_Record_Input; </code></pre> <p>In your implementation of <code>Discriminated_Record_Input</code>, you can get around the discriminant issue by doing everything in the declaration section, or using a local declare block. (warning: uncompiled code)</p> <pre><code>function Discriminated_Record_Input (Stream : access Ada.Streams.Root_Stream_Type'class) return Discriminated_Record is Size : constant Natural := Natural'input(Stream); Data : constant Discriminated_Record_Input := (Size, (others =&gt; Byte_Type'input(Stream)); begin return Data; end Discriminated_Record_Input; </code></pre> <p>The main drawback to this is that your data may get copied twice (once into the local constant, then once again from there into <code>MyObject</code>). A good optimizer might fix this, as would adding lvalue references to the language (but I don't know if that's being contemplated).</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload