On 04/11/2010 09:36 PM, bearophile wrote:
But those list-like data structures probably can't be defined using the normal 
array literals :-)

I was wrong, this is not so bad looking:

struct List(T) {
     // Node of std.range.SListRange is not static!
     static struct Node {
         T data;
         Node* next;
         this(T d, Node* p=null) {
             data = d;
             next = p;
         }
     }

     Node* lh;

     this(T[] arr) {
         foreach_reverse (el; arr)
             lh = new Node(el, lh);
     }
}

void main() {
     List!int items = [1, 2, 3];
}

Bye,
bearophile

It won't work for classes, though.

I'd think there'd be more trouble with other parts of the language though, like slices. foreach with index is another thing I care about particularly, but it should be trivial to implement. I can't recall what else is still special-cased with array literals.

And I agree about integer literals. What would be nice is a way to override how the literals are interpreted. Maybe some sort of macroish facility that pattern matches and manipulates the AST at compile time...

<dreaming>

macro @(<Decl>: @(<Type> is X)
                @(<Identifier>)
                =
                @(<IntegerLiteral>, lit), decl){
  string str = "`" ~ lit.getText() ~ "`";
  lit = @( str2X( $(<Exp>, str) ));
  return decl;
}

Ick, that turned into a mess fast.

</dreaming>

Reply via email to