Revised RFC on range design for D2
Sergey Gromov
snake.scaly at gmail.com
Wed Sep 17 07:08:39 PDT 2008
Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:
> Sergey Gromov wrote:
> > What you basically want to achieve is that
> >
> > Lockstep l1, l2;
> > l1.head = l2.head;
> >
> > translates into
> >
> > l1._r0.head = l2._r0.head;
> > l1._r1.head = l2._r1.head;
> >
> > all by itself. Umm... The simplest for a programmer would be compiler-
> > supported multiple return values and multiple assignment:
> >
> > ref typeof(R0.head), ref typeof(R1.head) head()
> > {
> > return _r0.head, _r1.head;
> > }
> >
> > then
> >
> > l1.head = l2.head
> >
> > is actually
> >
> > l1._r0.head, l1._r1.head = l2._r0.head, l2._r1.head;
> >
> > I'm not expecting this to be implemented though. Other methods,
> > including returning a Tulpe!(), all return a struct. There's no use in
> > returning references there, even if we could, as long as structs are
> > bit-copied. All the tricks with reference fields rely substantially on
> > the compiler performing specific actions on a per-field basis.
>
> I figured a deceptively simple(r) solution. With apologies to Abba, I
> let the code speak:
>
> /**
> Defines a range that moves two given ranges in lockstep.
> */
> struct Lockstep(R0, R1)
> {
> struct ElementType
> {
> private R0 _r0;
> private R1 _r1;
> auto _0() { return _r0.head; }
> auto _1() { return _r1.head; }
> }
> private ElementType _e;
>
> this(R0 r0, R1 r1)
> {
> _e._r0 = r0;
> _e._r1 = r1;
> }
>
> bool empty()
> {
> return _e._r0.empty || _e._r1.empty;
> }
>
> ref ElementType head()
> {
> return _e;
> }
>
> void next()
> {
> _e._r0.next;
> _e._r1.next;
> }
> }
Yes it should work. I wish it were less complex though. BTW here's
another variant of your solution:
> struct Lockstep(R0, R1)
> {
> private R0 _r0;
> private R1 _r1;
>
> private alias typeof(this) this_type;
>
> struct ElementType
> {
> private this_type outer;
> typeof(outer._r0.head) _0() { return outer._r0.head; }
> typeof(outer._r0.head) _1() { return outer._r1.head; }
> }
>
> this(R0 r0, R1 r1)
> {
> _r0 = r0;
> _r1 = r1;
> }
>
> bool empty()
> {
> return _r0.empty || _r1.empty;
> }
>
> ElementType head()
> {
> return ElementType(this);
> }
>
> void next()
> {
> _r0.next;
> _r1.next;
> }
> }
More information about the Digitalmars-d-announce
mailing list