Why after writeln the binaryHeap become empty?
Jonathan M Davis
newsgroup.d at jmdavisprog.com
Wed Jun 19 06:00:28 UTC 2019
On Tuesday, June 18, 2019 10:27:46 PM MDT lili via Digitalmars-d-learn
wrote:
> On Tuesday, 18 June 2019 at 17:25:51 UTC, Johannes Loher wrote:
> > The result of heapify is a BinaryHeap, which is a range. writeln
> > basically prints ranges by iterating over them and printing
> > each element
> > (except for the types which are special cased, such as dynamic
> > arrays
> > etc.). However, ranges are consumed by iterating over them,
> > which
> > explains the behavior because writeln is not special cased for
> > BinaryHeaps.
> >
> > Funnily enough, BinaryHeap does not implement a "save" method,
> > which is the usual way of making ranges copiable, i.e. making
> > them ForwardRanges. In this case I believe save could even
> > simply be an alias to dup.
>
> Do you known reason for why Dlang Range are consumed by iterating
> over them. I this design is strange.
If you want an overview of ranges, you can watch this:
https://www.youtube.com/watch?v=A8Btr8TPJ8c
You can also read this:
http://ddili.org/ders/d.en/ranges.html
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list