www.digitalmars.com         C & C++   DMDScript  

D - [HOWTO](MN002, part 4) Coding typesafe multi parameter functions, optional parameters and the like

reply Manfred Nowak <svv1999 hotmail.com> writes:
Part 4 of how to code multi parameter functions, optional parameters,
positional parameters, default valued parameters and the like, using the
the syntax extension I just suggested.

With working example using the currently available syntax.


In part 3 I presented two simple examples of FSM for processing multi
parameter lists. Now to some more advanced stuff.

4) A fully(?) fledged example for processing an actual parameter list, in
which is only one type allowed.

T is the type of the parameters of the actual parameter list. R is the
type of the result of processing the actual parameters. Data is the type
of the data, that must be hold during the processing of the list of
parameters. Power is the type of the functions for pre and post
processing. Running is the type for the function that does the processing
for each supplied parameter.

The latter two must be delegate( data, R) and delegate(T, Data, R). So
they might be replaced by interfaces.

The instantiation of the class must provide the function for pre and post
processing and the function for the processing of the actual parameter. 

<code>
static this(){
  printf("MNlist v0.1 prealpha loaded. (C) 2004 by Manfred Nowak.\n");
};

class MNlist(T, R, Data, Power, Running){
  static{
    bit opEOMexpected= false;  // for fake opEOM Data data;
    Power powerUp, powerDown;
    Running running;
    R result;
  }

  this( Power pu, Running r, Power pd){
    debug printf("[this ");
    powerUp= pu;
    running= r;
    powerDown= pd;
    debug printf("] ");
  }
  
  State0 opCall(T elem){  // fake opMultArg
    debug printf("[opMultArg( elem) ");
    assert( !opEOMexpected); // for fake opEOM
    opEOMexpected= true;  //for fake opEOM
    powerUp( data, result);
    debug printf("] ");
    return state0( elem);
  }

  R opCall( ){  // fake opMultArg
    debug printf("[opMultArg() ");
    assert( !opEOMexpected); // for fake opEOM
    opEOMexpected= true;  // for fake opEOM
    powerUp( data, result);
    debug printf("] ");
    return state0();
  }
   
    struct State0{
       State0 opCall(T elem){
          debug printf("[opCall( elem) ");
          running( elem, data, result);
          debug printf("] ");
          return *this;
       }
       R opCall(){ // fake opEOM
          debug printf("[opEOM( ) ");
          opEOMexpected= false; // for fake opEOM
          powerDown( data, result);
          debug printf("] ");
          return result;
       }
    }
    static State0 state0;
}
</code>

And here are some examples to use it:
- the already known print function
- a function to sum the parameters up
- a function, that converts the parameter list into an array of same
length, that holds the squares of the supplied arguments.

<code>
void main(){
  void print(){
    alias int T;
    alias void* R;
    alias bit Data;
    alias void delegate( inout Data,inout  R) Power;
    void puf( inout Data dta, inout R result){
      printf("(");
      dta= true;
    };
    Power pu; pu= &puf;
    void pdf( inout Data dta, inout R result){
      printf(")\n");
    }
    Power pd; pd= &pdf;
    alias void delegate(T , inout Data, inout R) Running; Running r;
    void rf( T elem, inout Data dta, inout R result) {
      debug printf("[running( elem, dta) "); char[] fmt;
      if( dta)
        fmt= "%d", dta= false;
      else
        fmt= ", %d";
      printf( fmt, elem);
      debug printf("] ");
    }
    r= &rf;
    alias MNlist!( T, R, Data, Power, Running) ListInt;
    ListInt list= new ListInt( pu, r, pd);
    
    printf("[print\n");
    list();
    list( 1)();
    list( 2)( 3)();
    list( 4)( 5)( 6)();
    printf("]\n");
  }

  void sum(){
    alias int T;
    alias int R;
    alias void* Data;
    alias void delegate( inout Data,inout  R) Power;
    Power pu= delegate ( inout Data dta,inout  R result) { result= 0;};
    Power pd= delegate ( inout Data dta,inout  R result) { };
    alias void delegate(T , inout Data, inout R) Running;
    Running r= delegate ( T elem, inout Data dta, inout R result) {
      debug printf("[running( elem, dta) "); result+= elem; debug
      printf("] ");
    };
    alias MNlist!( T, R, Data, Power, Running) ListInt;
    ListInt list= new ListInt( pu, r, pd);
    
    printf("[sum\n");
    list();
    list( 1)();
    list( 2)( 3)();
    printf("%d\n", list( 4)( 5)( 6)());
    printf("]\n");
  }

  void array(){
    alias int T;
    alias int[] R;
    alias void* Data;
    alias void delegate( inout Data,inout  R) Power;
    Power pu= delegate ( inout Data dta, inout R result) {
      result.length= 50;
      result.length= 0;
    };
    Power pd= delegate ( inout Data dta, inout R result) { };
    alias void delegate( T, inout Data, inout R) Running;
    Running r= delegate ( T elem, inout Data dta, inout R result) {
      debug printf("[running( elem, dta) ");
      result.length= result.length + 1;
      result[ result.length - 1]= elem * elem;
      debug printf("] ");
    };

    alias MNlist!( T, R, Data, Power, Running) ListInt;
    ListInt list= new ListInt( pu, r, pd);
    
    printf("[array\n");
    int[] arr;
    list();
    list( 1)();
    list( 2)( 3)();
    arr= list( 4)( 5)( 6)();
    for( int i=0; i< arr.length; i++){
      printf("%d\n", arr[i]);
    }
    printf("]\n");
  }
  printf("[main\n");

  print();
  sum();
  array();

  printf("]\n");
}
</code>

Enjoy.

To be continued.

So long!
Apr 02 2004
parent larry cowan <larry_member pathlink.com> writes:
Code is missing one line "Data data" as inserted below, then it will work.

In article <c4kr28$2mdb$1 digitaldaemon.com>, Manfred Nowak says...
Part 4 of how to code multi parameter functions, optional parameters,
positional parameters, default valued parameters and the like, using the
the syntax extension I just suggested.
..
<code>
static this(){
  printf("MNlist v0.1 prealpha loaded. (C) 2004 by Manfred Nowak.\n");
};

class MNlist(T, R, Data, Power, Running){
  static{
    bit opEOMexpected= false;  // for fake opEOM Data data;
    Power powerUp, powerDown;
    Running running;
    R result;
Data data;
  }
.. now Enjoy!
To be continued.

So long!
This is nice stuff!
Apr 03 2004