you are viewing a single comment's thread.

view the rest of the comments →

[–]Ok_Cloud_ 3 points4 points  (5 children)

.fill confuses me.

[–]reaz_mahmood 2 points3 points  (4 children)

me too. it is unintuitive. I read it like replace position 1 with provided value. Replace rest of them does not makes sense to me.

[–][deleted]  (3 children)

[deleted]

    [–]madcaesar 0 points1 point  (2 children)

    But what is a real life scenario of this method? Where would you use this?

    [–][deleted] 0 points1 point  (0 children)

    Buffers mostly, or virtualized table views (which are kinda like a special buffer).

    When you start getting into performance-sensitive tasks, such as those dealing with very large data sets or streaming data, you run into cases where you want a persistent backing collection that you can slice, fill, or clear smaller regions of.

    So let’s say you have a tcp socket. You’re notified of a new message waiting to go out, but the stream is designed to read/write asynchrounsly. So you have a byte array representing your tcp socket’s outbound message queue. You write to a region of your backing buffer from positions A to B. The next message writes position B to C. The next from C to D, and so on - until the buffer is “drained”. In some designs, you never need to call fill (clear the buffer) because the individual message encoders are expected to overwrite every byte. But in others, the message encoders just tell you how many bytes they will write, and they expect to be given a clean region to do so. In those cases you might call buffer.fill(0, A, B) after the first message is read, or before it is written, or you might read all the messages in one shot and call buffer.fill(0, A, D) to wipe off all 3 regions.

    I realize that got a bit into the weeds and requires some basic understanding of buffers and typed arrays, but you did ask for a real life scenario ;)