Thread overview
DMD range support?
Jan 10, 2009
Sergey Gromov
Jan 11, 2009
Sergey Gromov
Jan 12, 2009
Sergey Gromov
Jan 11, 2009
Sergey Gromov
January 10, 2009
DMD supports ranges in foreach for more than a month, but I still cannot find any docs or at least examples of the range syntax.  Lots of syntaxes were discussed and it's absolutely not obvious which of them actually got into the compiler.

Is this page still relevant? http://ssli.ee.washington.edu/~aalexand/d/tmp/std_range.html
January 10, 2009
On Sat, Jan 10, 2009 at 12:36 PM, Sergey Gromov <snake.scaly@gmail.com> wrote:
> DMD supports ranges in foreach for more than a month, but I still cannot find any docs or at least examples of the range syntax.  Lots of syntaxes were discussed and it's absolutely not obvious which of them actually got into the compiler.
>
> Is this page still relevant? http://ssli.ee.washington.edu/~aalexand/d/tmp/std_range.html
>

Maybe it just means the ranges in std.range got an opApply, or some kind of special treatment by the compiler?
January 10, 2009
Sergey Gromov wrote:
> DMD supports ranges in foreach for more than a month, but I still cannot
> find any docs or at least examples of the range syntax.  Lots of
> syntaxes were discussed and it's absolutely not obvious which of them
> actually got into the compiler.
> 
> Is this page still relevant?
> http://ssli.ee.washington.edu/~aalexand/d/tmp/std_range.html

Percolating!

Andrei
January 11, 2009
Sat, 10 Jan 2009 13:53:44 -0500, Jarrett Billingsley wrote:

> On Sat, Jan 10, 2009 at 12:36 PM, Sergey Gromov <snake.scaly@gmail.com> wrote:
>> DMD supports ranges in foreach for more than a month, but I still cannot find any docs or at least examples of the range syntax.  Lots of syntaxes were discussed and it's absolutely not obvious which of them actually got into the compiler.
>>
>> Is this page still relevant? http://ssli.ee.washington.edu/~aalexand/d/tmp/std_range.html
>>
> 
> Maybe it just means the ranges in std.range got an opApply, or some kind of special treatment by the compiler?

No, the following works:

struct Generator(int start, int step, int end)
{
  private int head_ = start;
  int head() { return head_; }
  void next() { head_ += step; }
  bool empty() { return head_ > end; }
}

import std.stdio;

void main()
{
  Generator!(2, 7, 50) gen;
  foreach (i; gen)
    writefln(i);
}

so at least the test method is empty(), not done().  That's a relief.
OTOH it does not work if the head is an actual field:

struct Generator(int start, int step, int end)
{
  int head = start;
  void next() { head += step; }
  bool empty() { return head > end; }
}

If you use it with foreach (i; gen) you get

  Error: cannot infer type for i

and for foreach (int i; gen):

  Error: no property 'opApply' for type 'Generator!(2,7,50)'
  Error: function expected before (), not 1 of type int
January 11, 2009
Sat, 10 Jan 2009 12:15:22 -0800, Andrei Alexandrescu wrote:

> Sergey Gromov wrote:
>> DMD supports ranges in foreach for more than a month, but I still cannot find any docs or at least examples of the range syntax.  Lots of syntaxes were discussed and it's absolutely not obvious which of them actually got into the compiler.
>> 
>> Is this page still relevant? http://ssli.ee.washington.edu/~aalexand/d/tmp/std_range.html
> 
> Percolating!

What do you mean?  It's in release notes for DMD 2.021.
January 11, 2009
Sergey Gromov wrote:
> Sat, 10 Jan 2009 12:15:22 -0800, Andrei Alexandrescu wrote:
> 
>> Sergey Gromov wrote:
>>> DMD supports ranges in foreach for more than a month, but I still cannot
>>> find any docs or at least examples of the range syntax.  Lots of
>>> syntaxes were discussed and it's absolutely not obvious which of them
>>> actually got into the compiler.
>>>
>>> Is this page still relevant?
>>> http://ssli.ee.washington.edu/~aalexand/d/tmp/std_range.html
>> Percolating!
> 
> What do you mean?  It's in release notes for DMD 2.021.

I mean the related overhaul of std.algorithm is in the works.

Andrei
January 12, 2009
"Sergey Gromov" wrote
> Sat, 10 Jan 2009 13:53:44 -0500, Jarrett Billingsley wrote:
>
>> On Sat, Jan 10, 2009 at 12:36 PM, Sergey Gromov <snake.scaly@gmail.com> wrote:
>>> DMD supports ranges in foreach for more than a month, but I still cannot find any docs or at least examples of the range syntax.  Lots of syntaxes were discussed and it's absolutely not obvious which of them actually got into the compiler.
>>>
>>> Is this page still relevant? http://ssli.ee.washington.edu/~aalexand/d/tmp/std_range.html
>>>
>>
>> Maybe it just means the ranges in std.range got an opApply, or some kind of special treatment by the compiler?
>
> No, the following works:
>
> struct Generator(int start, int step, int end)
> {
>  private int head_ = start;
>  int head() { return head_; }
>  void next() { head_ += step; }
>  bool empty() { return head_ > end; }
> }
>
> import std.stdio;
>
> void main()
> {
>  Generator!(2, 7, 50) gen;
>  foreach (i; gen)
>    writefln(i);
> }

I'd really like to know the code generation -- A delegate probably isn't generated anymore?  I agree more documentation is sorely needed in the spec.


January 12, 2009
Mon, 12 Jan 2009 11:43:11 -0500, Steven Schveighoffer wrote:

> "Sergey Gromov" wrote
>> struct Generator(int start, int step, int end)
>> {
>>  private int head_ = start;
>>  int head() { return head_; }
>>  void next() { head_ += step; }
>>  bool empty() { return head_ > end; }
>> }
>>
>> import std.stdio;
>>
>> void main()
>> {
>>  Generator!(2, 7, 50) gen;
>>  foreach (i; gen)
>>    writefln(i);
>> }
> 
> I'd really like to know the code generation -- A delegate probably isn't generated anymore?  I agree more documentation is sorely needed in the spec.

non-optimized asm:

__Dmain	comdat
	assume	CS:__Dmain
L0:		enter	4,0
		mov	EAX,_D4test24__T9GeneratorVi2Vi7Vi50Z9Generator6__initZ
		mov	-4[EBP],EAX
		mov	-4[EBP],EAX
LF:		lea	EAX,-4[EBP]
		call	near ptr _D4test24__T9GeneratorVi2Vi7Vi50Z9Generator5emptyMFZb
		xor	AL,1
		je	L32
		lea	EAX,-4[EBP]
		call	near ptr _D4test24__T9GeneratorVi2Vi7Vi50Z9Generator4headMFZi
		call	near ptr _D3std5stdio15__T8writeflnTiZ8writeflnFiZv
		lea	EAX,-4[EBP]
		call	near ptr _D4test24__T9GeneratorVi2Vi7Vi50Z9Generator4nextMFZv
		jmp short	LF
L32:		xor	EAX,EAX
		leave
		ret
__Dmain	ends

optimized asm (-O -release -inline):

__Dmain	comdat
	assume	CS:__Dmain
L0:		push	EAX
		mov	EAX,_D4test24__T9GeneratorVi2Vi7Vi50Z9Generator6__initZ
		cmp	EAX,032h
		mov	[ESP],EAX
		jg	L25
LE:		push	[ESP]
		mov	EAX,0Ah
		call	near ptr _D3std5stdio15__T6writefTiTaZ6writefFiaZv
		add	[ESP],7
		cmp	[ESP],032h
		jle	LE
L25:		pop	ECX
		xor	EAX,EAX
		ret
__Dmain	ends

*Pretty* nice.