FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

October 26th - foreach/opApply, API improvements, formatting

 
Post new topic   Reply to topic     Forum Index -> MiniD
View previous topic :: View next topic  
Author Message
JarrettBillingsley



Joined: 20 Jun 2006
Posts: 457
Location: Pennsylvania!

PostPosted: Thu Oct 26, 2006 9:48 pm    Post subject: October 26th - foreach/opApply, API improvements, formatting Reply with quote

Today was quite productive! Here are the new features:

  • A new metamethod, opApply, has been added. This is used when the container of a foreach loop is not a function or delegate -- the opApply is called in order to obtain an iterator for the loop. This allows you to write intuitive-looking foreach loops (rather than explicitly writing the iterator). In addition, the opApply function can take two parameters -- the container to iterate over (i.e. the "this" pointer), and an extra parameter which can be used for just about anything. The standard library uses the extra parameter to indicate the direction of the iteration for arrays and strings -- pass in "reverse" and the iteration goes backwards. Who needs foreach_reverse?

    Here's an example of the iteration in action:

    Code:
    class Test
    {
       mData = [4, 5, 6];

       method opApply(extra)
       {
          function iterator(this, index)
          {
             ++index;
             
             if(index >= #this.mData)
                return;
                
             return index, this.mData[index];
          }
          
          function iterator_reverse(this, index)
          {
             --index;
             
             if(index < 0)
                return;
                
             return index, this.mData[index];
          }

          if(isString(extra) && extra == "reverse")
             return iterator_reverse, this, #this.mData;
          else
             return iterator, this, -1;
       }
    }

    local t = Test();

    foreach(local k, local v; t)
       writefln("t[", k, "] = ", v);
       
    writefln();

    foreach(local k, local v; t, "reverse")
       writefln("t[", k, "] = ", v);
       
    t =
    {
       fork = 5,
       knife = 10,
       spoon = "hi"
    };

    writefln();

    foreach(local k, local v; t)
       writefln("t[", k, "] = ", v);
       
    t = [5, 10, "hi"];

    writefln();

    foreach(local k, local v; t)
       writefln("t[", k, "] = ", v);

    writefln();

    foreach(local k, local v; t, "reverse")
       writefln("t[", k, "] = ", v);
       
    writefln();

    local s = "hello";

    foreach(local k, local v; s)
       writefln("s[", k, "] = ", v);

    writefln();

    foreach(local k, local v; s, "reverse")
       writefln("s[", k, "] = ", v);


    This produces:

    Code:
    t[0] = 4
    t[1] = 5
    t[2] = 6

    t[2] = 6
    t[1] = 5
    t[0] = 4

    t[knife] = 10
    t[spoon] = hi
    t[fork] = 5

    t[0] = 5
    t[1] = 10
    t[2] = hi

    t[2] = hi
    t[1] = 10
    t[0] = 5

    s[0] = h
    s[1] = e
    s[2] = l
    s[3] = l
    s[4] = o

    s[4] = o
    s[3] = l
    s[2] = l
    s[1] = e
    s[0] = h


    There are default opApply methods defined for strings, tables, and arrays, and you can define your own for classes, as shown.

  • There have been some improvements / changes to the API. Notably, the "isTypeParam" functions in MDState have all been compressed into one templated function, so what was "isIntParam(0)" before is now "isParam!("int")(0)". It's a little wordier, but saves a lot of space in the code. Additionally, the "getTypeParam" functions have been extended to all types (some were missing before), and "getUpvalue" and "setUpvalue" have been added for accessing upvalues in native function closures. (An example usage of this can be found in tablelib.d, for the table iterator and apply functions.)

  • D-style formatting is now available! There is now a new base library function, "format", which looks and acts just like its D counterpart. Most formatting specifiers, flags, etc. work just as they do with D's std.format, but there are a few specifiers which don't have any meaning to MiniD (i.e. ?p) which will flag as errors. Additionally, variable precision and width specifiers (i.e. the '*' in "?.*d") are illegal (they're really tricky and I've never seen them used anyway). But now writefln() and writef() work just as they do in D. Additionally, there are two new output functions, writeln() and write(), which just write out their arguments without any formatting (and strings are written so as not to be taken as format strings).


So that's what got done today. I'm very happy with it Smile
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic     Forum Index -> MiniD All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group