www.digitalmars.com         C & C++   DMDScript  

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

reply Manfred Nowak <svv1999 hotmail.com> writes:
Part 3 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 2 I introduced the general way to code a finite state machine
(FSM) for accepting a regular expression (RE) over a signature alphabet.
Now to the working examples.

3) Let us start with a simple FSM that does nothing but analyze the actual
call whether it conforms to the regular expression as implemented by the
FSM. Such FSM is called acceptor.

Because I promised a working example with the current syntax and currently
there is no opMultArg it must be faked.

3.a) The following code implements an acceptor for the signature S=int and
the regular expression r=S*:

<code>
class List{
  static:
  State0 opCall(int elem){  // fake opMultArg
    return state0( elem);
  }
  void opCall(){  // fake opMultArg
  }
  struct State0{
    State0 opCall(int elem){
      State0 next; return next;
    }
    void opCall(){
    }
  }
  State0 state0;
}

/*
Because I promised a working example with the current syntax and there is
no semicolon allowed to separate signatures the semicolon must be faked
with `)(', i.e. a right followed by a left parenthesis. */

void main(){
  List list= new List;

  list();
  list( 1);
  list( 1)( 2);
  list( 1)( 2)( 3);
}
</code>

Play around with this. Replace the numbers with double, real, char etc.
and watch the compiler rejecting your plays.

From the code you may have noticed, that you are able to extend  it, so
that it does some preprocessing, by introducing for example some printf's
into the bodies of the faked opMultArg's. You can also introduce printf's
into the opCall's of state0, to print out the values of the actual
parameters provided. But there is no way to post process the data received
because there is no opEOM.

The opEOM must be faked also, by appending a signature, that is not part
of the signatures that are accepted regularly. Because the coder of the
call might forget this appending, it must be checked for. Naturally this
checking must be delayed until the next call of the multi parameter
function and the appending of the faked opEOM to the very last call can be
checked by the destructor of the class. For the empty list of actual
parameters there is no need to append a fake opEOM, because the  post
processing can be done immediately.

The last fact enables us to use the empty signature for signaling the end
of the actual parameter list.

3.b) The following code implements a FSM that accepts lists of integers
and prints out their values in octal notation separated by commas and
surrounded by parentheses.

<code>
class List{
  static:
  bit opEOMexpected= false; // for fake opEOM
  bit firstParameter;  // for the special handling
                       // of the first parameter

  State0 opCall(int elem){  // fake opMultArg
    assert( !opEOMexpected); // for fake opEOM
    opEOMexpected= true;  //for fake opEOM
    printf("(");
    firstParameter= true;
    return state0( elem);
  }

  void opCall( ){  // fake opMultArg
    assert( !opEOMexpected); // for fake opEOM
    printf("()\n");
  }
   
  struct State0{
    State0 opCall(int elem){
      if( firstParameter)
        printf("%X", elem);
      else
        printf(", %X", elem);
      firstParameter= false;
      State0 next; return next;
    }
    void opCall(){ // fake opEOM
      printf(")\n");
      opEOMexpected= false; // for fake opEOM
    }
  }
  State0 state0;
}
void main(){
  List list= new List;

  list();
  list( 100)();
  list( 100)( 200)();
  list( 100)( 200)( 300)();
}
</code>

Again you might want to play around with this.

To be continued.

So long!
Apr 02 2004
parent reply C <dont respond.com> writes:
MW, can you post these somewhere ?  I'm usually a fan of the NG , but =

reading howto-s off of it is not so fun.  Wiki or Dsource ?

Thanks ;),
C

On Fri, 02 Apr 2004 18:00:21 +0200, Manfred Nowak <svv1999 hotmail.com> =

wrote:

 Part 3 of how to code multi parameter functions, optional parameters,
 positional parameters, default valued parameters and the like, using t=

 the syntax extension I just suggested.

 With working example using the currently available syntax.


 In part 2 I introduced the general way to code a finite state machine
 (FSM) for accepting a regular expression (RE) over a signature alphabe=

 Now to the working examples.

 3) Let us start with a simple FSM that does nothing but analyze the =

 actual
 call whether it conforms to the regular expression as implemented by t=

 FSM. Such FSM is called acceptor.

 Because I promised a working example with the current syntax and =

 currently
 there is no opMultArg it must be faked.

 3.a) The following code implements an acceptor for the signature S=3Di=

 and
 the regular expression r=3DS*:

 <code>
 class List{
   static:
   State0 opCall(int elem){  // fake opMultArg
     return state0( elem);
   }
   void opCall(){  // fake opMultArg
   }
   struct State0{
     State0 opCall(int elem){
       State0 next; return next;
     }
     void opCall(){
     }
   }
   State0 state0;
 }

 /*
 Because I promised a working example with the current syntax and there=

 no semicolon allowed to separate signatures the semicolon must be fake=

 with `)(', i.e. a right followed by a left parenthesis. */

 void main(){
   List list=3D new List;

   list();
   list( 1);
   list( 1)( 2);
   list( 1)( 2)( 3);
 }
 </code>

 Play around with this. Replace the numbers with double, real, char etc=

 and watch the compiler rejecting your plays.

 From the code you may have noticed, that you are able to extend  it, s=

 that it does some preprocessing, by introducing for example some print=

 into the bodies of the faked opMultArg's. You can also introduce print=

 into the opCall's of state0, to print out the values of the actual
 parameters provided. But there is no way to post process the data =

 received
 because there is no opEOM.

 The opEOM must be faked also, by appending a signature, that is not pa=

 of the signatures that are accepted regularly. Because the coder of th=

 call might forget this appending, it must be checked for. Naturally th=

 checking must be delayed until the next call of the multi parameter
 function and the appending of the faked opEOM to the very last call ca=

 be
 checked by the destructor of the class. For the empty list of actual
 parameters there is no need to append a fake opEOM, because the  post
 processing can be done immediately.

 The last fact enables us to use the empty signature for signaling the =

 of the actual parameter list.

 3.b) The following code implements a FSM that accepts lists of integer=

 and prints out their values in octal notation separated by commas and
 surrounded by parentheses.

 <code>
 class List{
   static:
   bit opEOMexpected=3D false; // for fake opEOM
   bit firstParameter;  // for the special handling
                        // of the first parameter

   State0 opCall(int elem){  // fake opMultArg
     assert( !opEOMexpected); // for fake opEOM
     opEOMexpected=3D true;  //for fake opEOM
     printf("(");
     firstParameter=3D true;
     return state0( elem);
   }

   void opCall( ){  // fake opMultArg
     assert( !opEOMexpected); // for fake opEOM
     printf("()\n");
   }

   struct State0{
     State0 opCall(int elem){
       if( firstParameter)
         printf("%X", elem);
       else
         printf(", %X", elem);
       firstParameter=3D false;
       State0 next; return next;
     }
     void opCall(){ // fake opEOM
       printf(")\n");
       opEOMexpected=3D false; // for fake opEOM
     }
   }
   State0 state0;
 }
 void main(){
   List list=3D new List;

   list();
   list( 100)();
   list( 100)( 200)();
   list( 100)( 200)( 300)();
 }
 </code>

 Again you might want to play around with this.

 To be continued.

 So long!

-- = D Newsgroup.
Apr 02 2004
next sibling parent reply C <dont respond.com> writes:
err, MN :)

On Fri, 02 Apr 2004 11:25:58 -0800, C <dont respond.com> wrote:

 MW, can you post these somewhere ?  I'm usually a fan of the NG , but =

 reading howto-s off of it is not so fun.  Wiki or Dsource ?

 Thanks ;),
 C

 On Fri, 02 Apr 2004 18:00:21 +0200, Manfred Nowak <svv1999 hotmail.com=
 =

 wrote:

 Part 3 of how to code multi parameter functions, optional parameters,=


 positional parameters, default valued parameters and the like, using =


 the syntax extension I just suggested.

 With working example using the currently available syntax.


 In part 2 I introduced the general way to code a finite state machine=


 (FSM) for accepting a regular expression (RE) over a signature alphab=


 Now to the working examples.

 3) Let us start with a simple FSM that does nothing but analyze the =


 actual
 call whether it conforms to the regular expression as implemented by =


 FSM. Such FSM is called acceptor.

 Because I promised a working example with the current syntax and =


 currently
 there is no opMultArg it must be faked.

 3.a) The following code implements an acceptor for the signature S=3D=


 and
 the regular expression r=3DS*:

 <code>
 class List{
   static:
   State0 opCall(int elem){  // fake opMultArg
     return state0( elem);
   }
   void opCall(){  // fake opMultArg
   }
   struct State0{
     State0 opCall(int elem){
       State0 next; return next;
     }
     void opCall(){
     }
   }
   State0 state0;
 }

 /*
 Because I promised a working example with the current syntax and ther=


 is
 no semicolon allowed to separate signatures the semicolon must be fak=


 with `)(', i.e. a right followed by a left parenthesis. */

 void main(){
   List list=3D new List;

   list();
   list( 1);
   list( 1)( 2);
   list( 1)( 2)( 3);
 }
 </code>

 Play around with this. Replace the numbers with double, real, char et=


 and watch the compiler rejecting your plays.

 From the code you may have noticed, that you are able to extend  it, =


 that it does some preprocessing, by introducing for example some =


 printf's
 into the bodies of the faked opMultArg's. You can also introduce =


 printf's
 into the opCall's of state0, to print out the values of the actual
 parameters provided. But there is no way to post process the data =


 received
 because there is no opEOM.

 The opEOM must be faked also, by appending a signature, that is not p=


 of the signatures that are accepted regularly. Because the coder of t=


 call might forget this appending, it must be checked for. Naturally t=


 checking must be delayed until the next call of the multi parameter
 function and the appending of the faked opEOM to the very last call c=


 be
 checked by the destructor of the class. For the empty list of actual
 parameters there is no need to append a fake opEOM, because the  post=


 processing can be done immediately.

 The last fact enables us to use the empty signature for signaling the=


 end
 of the actual parameter list.

 3.b) The following code implements a FSM that accepts lists of intege=


 and prints out their values in octal notation separated by commas and=


 surrounded by parentheses.

 <code>
 class List{
   static:
   bit opEOMexpected=3D false; // for fake opEOM
   bit firstParameter;  // for the special handling
                        // of the first parameter

   State0 opCall(int elem){  // fake opMultArg
     assert( !opEOMexpected); // for fake opEOM
     opEOMexpected=3D true;  //for fake opEOM
     printf("(");
     firstParameter=3D true;
     return state0( elem);
   }

   void opCall( ){  // fake opMultArg
     assert( !opEOMexpected); // for fake opEOM
     printf("()\n");
   }

   struct State0{
     State0 opCall(int elem){
       if( firstParameter)
         printf("%X", elem);
       else
         printf(", %X", elem);
       firstParameter=3D false;
       State0 next; return next;
     }
     void opCall(){ // fake opEOM
       printf(")\n");
       opEOMexpected=3D false; // for fake opEOM
     }
   }
   State0 state0;
 }
 void main(){
   List list=3D new List;

   list();
   list( 100)();
   list( 100)( 200)();
   list( 100)( 200)( 300)();
 }
 </code>

 Again you might want to play around with this.

 To be continued.

 So long!


-- = D Newsgroup.
Apr 02 2004
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
"That's very important," the King said, turning to the jury. They were 
just beginning to write this down on their slates, when the White Rabbit 
interrupted: "Unimportant, your Majesty means, of course," he said, in a 
very respectful tone, but frowning and making faces at him as he spoke.

"Unimportant, of course, I meant," the King hastily said, and went on to 
himself in an under-tone, 
"important--unimportant--unimportant--important----" as if he were 
trying which word sounded best.

Some of the jury wrote it down "important," and some "unimportant." 
Alice could see this, as she was near enough to look over their slates; 
"but it doesn't matter a bit," she thought to herself.

-eye


Manfred Nowak schrieb:

 C wrote:
 
 
err, MN :)

Exchanging a single letter does not matter. Shortening a name to its initials does matter, even when one does it with its own name. Hiding behind an invalid email address does matter. Providing public critique without obvious interest to the public does matter. Exposing oneself as a net cop in a NG that welcomes even flame wars does matter. So long! P.S.: Please introduce me to your kill file.

Apr 02 2004
next sibling parent reply C <dont respond.com> writes:
:).

Theres a misunderstanding, I like the howto's, and I dont care that you =

post to the newsgroup.  Im asking in addition to the posting to the =

newsgroup, why don't you put them on the web so its easier to read , and=
 =

people can find it that aren't in the newsgroup.

So Long!
P.S. I only used MN because its in the subject of these HOWTO's

On Fri, 02 Apr 2004 21:20:16 +0200, Ilya Minkov <minkov cs.tum.edu> wrot=
e:

 "That's very important," the King said, turning to the jury. They were=

 just beginning to write this down on their slates, when the White Rabb=

 interrupted: "Unimportant, your Majesty means, of course," he said, in=

 very respectful tone, but frowning and making faces at him as he spoke=

 "Unimportant, of course, I meant," the King hastily said, and went on =

 himself in an under-tone, =

 "important--unimportant--unimportant--important----" as if he were =

 trying which word sounded best.

 Some of the jury wrote it down "important," and some "unimportant." =

 Alice could see this, as she was near enough to look over their slates=

 "but it doesn't matter a bit," she thought to herself.

 -eye


 Manfred Nowak schrieb:

 C wrote:


 err, MN :)

Exchanging a single letter does not matter. Shortening a name to its initials does matter, even when one does it =


 with
 its own name.

 Hiding behind an invalid email address does matter.

 Providing public critique without obvious interest to the public does=


 matter.

 Exposing oneself as a net cop in a NG that welcomes even flame wars d=


 matter.

 So long!

 P.S.:
 Please introduce me to your kill file.


-- = D Newsgroup.
Apr 02 2004
parent reply Ilya Minkov <minkov cs.tum.edu> writes:
C schrieb:

 :).

Nice i could do something good to this discussion which leads nowhere.
 Theres a misunderstanding, I like the howto's, and I dont care that you 
 post to the newsgroup.  Im asking in addition to the posting to the 
 newsgroup, why don't you put them on the web so its easier to read , and 
 people can find it that aren't in the newsgroup.

Then i think someone can simply add them to the Wiki4D. I think this need not be the Author, because the license for usage of this newsgroup includes, IIRC, that posted material can be used to promote D. I would do it myself if i wasn't that busy picking up a good quote. ;)
 So Long!
 P.S. I only used MN because its in the subject of these HOWTO's

I figured that out. Seems that Manfred has not. -eye
Apr 02 2004
parent John Reimer <jjreimer telus.net> writes:
<snip>

 Then i think someone can simply add them to the Wiki4D. I think this
 need not be the Author, because the license for usage of this newsgroup
 includes, IIRC, that posted material can be used to promote D. I would
 do it myself if i wasn't that busy picking up a good quote. ;)
 

You mean you weren't quoting from memory?! Ah man, you've just destroyed my admiration for you. ;-)
 So Long!
 P.S. I only used MN because its in the subject of these HOWTO's

I figured that out. Seems that Manfred has not. -eye

Well I must admit, it is a little odd to refer to a person by their initials in direct address (especially when their name is known). But this newsgroup is an odd place, so it's "unimportant." :-)
Apr 02 2004
prev sibling parent reply John Reimer <jjreimer telus.net> writes:
Ilya Minkov wrote:

 "That's very important," the King said, turning to the jury. They were
 just beginning to write this down on their slates, when the White Rabbit
 interrupted: "Unimportant, your Majesty means, of course," he said, in a
 very respectful tone, but frowning and making faces at him as he spoke.
 
 "Unimportant, of course, I meant," the King hastily said, and went on to
 himself in an under-tone,
 "important--unimportant--unimportant--important----" as if he were
 trying which word sounded best.
 
 Some of the jury wrote it down "important," and some "unimportant."
 Alice could see this, as she was near enough to look over their slates;
 "but it doesn't matter a bit," she thought to herself.
 
 -eye

He he. I remember my mother thinking there was absolutely something messed up in the mind of Lewis Carroll, but for some reason I always loved these books. As crazy as they were, they always seemed full of good wholsome satire. Though posting this here may just be unnecessarily adding fuel to the fire...
Apr 02 2004
parent Ilya Minkov <minkov cs.tum.edu> writes:
John Reimer schrieb:

 He he.
 
 I remember my mother thinking there was absolutely something messed up in
 the mind of Lewis Carroll, but for some reason I always loved these books. 
 As crazy as they were, they always seemed full of good wholsome satire.
 
 Though posting this here may just be unnecessarily adding fuel to the
 fire...

Whoops! I missed quotation marks! We can settle it easily. --- 8< --- At this moment the King, who had been for some time busily writing in his note-book, cackled out "Silence!" and read out from his book, "Rule Forty-two. ALL PERSONS MORE THAN A MILE HIGH TO LEAVE THE COURT." --- >8 --- which also means that everyone below that height has no reason to feel bad. ;) -eye
Apr 02 2004
prev sibling parent reply Manfred Nowak <svv1999 hotmail.com> writes:
C wrote:

[...]
 reading howto-s off of it is not so fun

Readers of this newsgroup have requested examples for my suggested syntax extension. After giving some theoretical or general introduction I am suplying these examples, with exactly this posting. Would you please explain, why it can be off topic to provide examples for the use of suggested syntax extensions? So long!
Apr 02 2004
next sibling parent reply John Reimer <jjreimer telus.net> writes:
Manfred Nowak wrote:

 C wrote:
 
 [...]
 reading howto-s off of it is not so fun

Readers of this newsgroup have requested examples for my suggested syntax extension. After giving some theoretical or general introduction I am suplying these examples, with exactly this posting. Would you please explain, why it can be off topic to provide examples for the use of suggested syntax extensions? So long!

Manfred, I think he just thought the posting of these essays on the Wiki would make it easier for people to read (nicer formatting and all). But, I have to admit, given the intent of your essays, they are probably more appropriate to post here for now as you said.
Apr 02 2004
parent C <dont respond.com> writes:
Yes I like the HOW-TO's , its not off topic.  I just thinking it would b=
e =

easier to read ( and would have a permanent place to reference them ) if=
  =

you put them on the web somewhere.


C

On Fri, 02 Apr 2004 10:35:52 -0800, John Reimer <jjreimer telus.net> wro=
te:

 Manfred Nowak wrote:

 C wrote:

 [...]
 reading howto-s off of it is not so fun

Readers of this newsgroup have requested examples for my suggested =


 syntax
 extension.

 After giving some theoretical or general introduction I am suplying =


 these
 examples, with exactly this posting.

 Would you please explain, why it can be off topic to provide examples=


 for
 the use of suggested syntax extensions?

 So long!

Manfred, I think he just thought the posting of these essays on the Wi=

 would make it easier for people to read (nicer formatting and all).

 But, I have to admit, given the intent of your essays, they are probab=

 more appropriate to post here for now as you said.

-- = D Newsgroup.
Apr 02 2004
prev sibling parent reply "Ben Hinkle" <bhinkle4 juno.com> writes:
"Manfred Nowak" <svv1999 hotmail.com> wrote in message
news:c4kb49$1uu6$1 digitaldaemon.com...
 C wrote:

 [...]
 reading howto-s off of it is not so fun

Readers of this newsgroup have requested examples for my suggested syntax extension. After giving some theoretical or general introduction I am suplying these examples, with exactly this posting.

I've been reading your posts about this and I have a quick question: if foo is declared with the ";" in the parameter list how many times is foo called when you use ";" at the call site? I had assumed foo was called once but the example you gave using opCall made me think it gets called again each time the user supplies a ";". If that is true then why not just use opCall? Is it to be able to write foo(1;2;3) instead of foo(1)(2)(3)
 Would you please explain, why it can be off topic to provide examples for
 the use of suggested syntax extensions?

I think the "off" referred to reading "off the newsgroup" and not "off-topic".
Apr 02 2004
next sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
Ben Hinkle wrote:

[...]
 if foo is declared with the ";" in the parameter list how many times
 is foo called when you use ";" at the call site?

I do not understand completely what the current compiler does, because the code returns a struct inside of foo, and not the representation of foo.
 I had assumed foo
 was called once but the example you gave using opCall made
 me think it gets called again each time the user supplies a ";".

Clearly the actual call of the according opCall is prepared when the compiler analyzes the actual signature list and finds a ";".
 If that is true then why not just use opCall? Is it to be able to write
 foo(1;2;3) instead of foo(1)(2)(3)

The ";" is nearly only syntax sugar. But in addition it enables the compiler to emit a request for opEOM, when it reaches the closing right parenthesis. With foo()()() there is no such implicit possibility and therefore an additional token must be appended to signal the end of the actual signature list. So long!
Apr 02 2004
prev sibling parent Manfred Nowak <svv1999 hotmail.com> writes:
Ben Hinkle wrote:

[...]
 I had assumed foo
 was called once but the example you gave using opCall made
 me think it gets called again each time the user supplies a ";".

To have a more clear picture I added a "= 0" to the right of such a call and the compiler spit out: 'list.opCall(1).opCall(2).opCall(3)' is not an lvalue Now it is quite obvious, that the compiler prepares only one call to `foo' aka `list'. So long!
Apr 05 2004