Component Programming in D

Piotr Szturmaj bncrbme at jadamspam.pl
Wed Oct 3 06:01:42 PDT 2012


Andrei Alexandrescu wrote:
> http://www.reddit.com/r/programming/comments/10u6sk/component_programming_in_d/

Nice article!

There, I found an interesting way of writing ranges that are not 
explicitly initialized:

     private import core.stdc.stdio;

     struct StdinByChar {

	@property bool empty() {
	    if (hasChar)
		return false;
	    auto c = fgetc(stdin);
	    if (c == EOF)
		return true;
	    ch = cast(char)c;
	    hasChar = true;
	    return false;
	}

	@property char front() {
	    return ch;
         }

	void popFront() {
	    hasChar = false;
	}

       private:
	char ch;
	bool hasChar;
     }

This is unusual to me that popFront() code is shifted to empty(), but I 
guess that's necessary because structs doesn't have () ctors.


More information about the Digitalmars-d-announce mailing list