www.digitalmars.com         C & C++   DMDScript  

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

reply Manfred Nowak <svv1999 hotmail.com> writes:
How to code typesafe 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.


1) All those features mentioned in the headline are special cases of the
more general case of regular expressions over an alphabet of signatures.


1.a) For the purpose of this writing signatures are considered to be the
ordered list of types that form the input parameters of a currently
codeable function, i.e. the list is of arbitrary but fixed length and the
return value is not considered to be part of the signature.


1.b) An alphabet of signatures is a finite set out of the virtual
unlimited space of possible signatures. For example all the signatures
that are coded in a given module/program/system make up an alphabet.


1.c) A (simple) regular expression consists of branches, alternatives and
repetitions.

1.c.i)Let n be a nonnegative number and let S1, S2, ... ,Sn be n
signatures out of a given alphabet of signatures, then by stringing them
together with semicola a branch is formed:
   S1; S2; ... Sn

The meaning of a branch is, that for every given index i, with 1<=i<n, the
signature Si must be followed by the signature S(i+1) to make the branch
match. If n==0, then the branch matches always.

This is similar to the parameter lists, that currently can be coded, with
the exception, that current parameter lists consist of types, not of
signatures.

By surrounding a branch with braces, it can be used like a signature.


1.c.ii) Let n be a natural number, with n>=2, and let B1, B2, ... , Bn be
n branches as defined before, then by stringing them together with
vertical bars an alternative is formed:
   B1 | B2 | ... | Bn

The meaning of an alternative is, that if for some index i, with 1<=i<=n,
the branch Bi matches, then the alternative matches.

By surrounding an alternative with braces it can be used like a signature.

Note: an alternative is just another description of the  well known
overloading of functions.


1.c.iii) Let S be a signature as defined before, then by appending a * a
repetition is formed:
   S*

The meaning of a repetition is, that the if the signature S is repeated an
arbitrary number of times, i.e. not fixed, the repetition is matched.

The repetition does not need braces to be used as a signature.


1.d) Examples

For being able to distinguish between types, that form signatures, and
signatures, types are separated by commas and signatures are separated by
semicola.


Let S be the empty signature, i.e. where no type is provided. Now consider
the regular expression r=S. Then the multi parameter list () is accepted
by r.

Let S be as before. Consider the regular expression r=S S. Then the multi
parameter list (;) is accepted by r.

Let S1 be the empty signature and S2=int. Consider the regular expression
r=(S1|S2). Then the multi parameter lists () and (int) are accepted by r.
Note again, that this is just overloading.

Let S1 and S2 be as before. Consider the regular expression
r=(S1|S2)(S1|S2). Then the multi parameter lists (;), (int;), (;int) and
(int;int) are accepted. Note that we now have positional parameters,
default valued parameters and the like.

Let S=int,int. Consider the regular expression r=S*. Then the multi
parameter lists (), (int,int), (int,int;int,int), ... are accepted by r.

To be continued.

So long!
Mar 31 2004
next sibling parent reply John Reimer <jjreimer telus.net> writes:
<snip tutorial>

Hey Manfred, these "HOWTO"s are great. Have you considered submitting 
them to www.dsource.org? --> (obviously under "advanced")
Mar 31 2004
parent reply Brad Anderson <brad dsource.dot.org> writes:
Yes, it would help us work on having text and code alternating on the 
Tutorials section, instead of all-code-in-one as it is now.

BA

John Reimer wrote:
 <snip tutorial>
 
 Hey Manfred, these "HOWTO"s are great. Have you considered submitting 
 them to www.dsource.org? --> (obviously under "advanced")

Mar 31 2004
parent reply J C Calvarese <jcc7 cox.net> writes:
Brad Anderson wrote:
 Yes, it would help us work on having text and code alternating on the 
 Tutorials section, instead of all-code-in-one as it is now.

I think this material might be better suited for Wiki4D, since the Wiki already supports custom text formatting and the dsource tutorials only handle syntax highlighing. I do plan on trying to add some formatting options to the tutorials section, but it might get tricky and I'm not sure how long it'll take. As it is right now, this could be put into the tutorials, but the non-code portions would need to enclosed in comments (/+ +/, /* */, or //).
 
 BA
 
 John Reimer wrote:
 
 <snip tutorial>

 Hey Manfred, these "HOWTO"s are great. Have you considered submitting 
 them to www.dsource.org? --> (obviously under "advanced")


-- Justin http://jcc_7.tripod.com/d/
Mar 31 2004
parent John Reimer <jjreimer telus.net> writes:
J C Calvarese wrote:

 Brad Anderson wrote:
 Yes, it would help us work on having text and code alternating on the
 Tutorials section, instead of all-code-in-one as it is now.

I think this material might be better suited for Wiki4D, since the Wiki already supports custom text formatting and the dsource tutorials only handle syntax highlighing. I do plan on trying to add some formatting options to the tutorials section, but it might get tricky and I'm not sure how long it'll take. As it is right now, this could be put into the tutorials, but the non-code portions would need to enclosed in comments (/+ +/, /* */, or //).

Oops! I guess I jumped the gun here. Sorry guys.
Apr 02 2004
prev sibling next sibling parent reply Ben Hinkle <bhinkle4 juno.com> writes:
1.d) Examples

For being able to distinguish between types, that form signatures, and
signatures, types are separated by commas and signatures are separated by
semicola.

Let S be the empty signature, i.e. where no type is provided. Now consider
the regular expression r=S. Then the multi parameter list () is accepted
by r.

Let S be as before. Consider the regular expression r=S S. Then the multi
parameter list (;) is accepted by r.

Let S1 be the empty signature and S2=int. Consider the regular expression
r=(S1|S2). Then the multi parameter lists () and (int) are accepted by r.
Note again, that this is just overloading.

Let S1 and S2 be as before. Consider the regular expression
r=(S1|S2)(S1|S2). Then the multi parameter lists (;), (int;), (;int) and
(int;int) are accepted. Note that we now have positional parameters,
default valued parameters and the like.

Let S=int,int. Consider the regular expression r=S*. Then the multi
parameter lists (), (int,int), (int,int;int,int), ... are accepted by r.

I've gone back are re-read your proposal using ; in parameter lists and I can't figure out what the ; would actually do. The earlier post described how ; affects evaluation order but didn't say much else. For example, how can I fill in the following pseudo-code: void foo(int a,int b;int c,int d) { // fill in example here } void bar() { foo( /* ???? */ ); } Without that I can't make heads or tails of why it would be important to write a regular expression to match these kinds of signatures. -Ben
Mar 31 2004
parent Manfred Nowak <svv1999 hotmail.com> writes:
Ben Hinkle wrote:

[...]
  void foo(int a,int b;int c,int d)
  {
    // fill in example here
  }
  void bar() { foo( /* ???? */ ); }
 
 Without that I can't make heads or tails of why it would be
 important to write a regular expression to match these kinds of
 signatures.

:-) With your example even the existence/introduction of the `...' notion could be made questionable. The power of the regular expression approach lies in the repetition of signatures, branches and alternatives. Your example misses this. For your example a regular expression is not needed, because the current syntax supports that: simply replace the `;' by a `,' and proceed as usual. However the example can be made more interesting, when in addition you want to be able to process (pseudo code again) one or more of this: void foo( ; int c, int d){ ... } or void foo( int a; int b, int c, int d){ ... } or void foo( int a, int b, int c; int d){ ... } You may have noticed, that there are eight possibilities how one can divide a sequence of four parameters into sequences of signatures. Therefore there are also eight possible interpretations. This is inherently dangerous. However, my main focus is on lists, which are supported by regular expressions. All other thingies are special cases of the general principle. Nobody is forced to use them. So long!
Mar 31 2004
prev sibling parent reply Andy Friesen <andy ikagames.com> writes:
Manfred Nowak wrote:

 How to code typesafe multi parameter functions, optional parameters,
 positional parameters, default valued parameters and the like, using the
 the syntax extension I just suggested.
 
 [ stuff ]

Maybe we can get Walter to incorporate these changes. -- andy
Mar 31 2004
parent Andy Friesen <andy ikagames.com> writes:
Andy Friesen wrote:
 Manfred Nowak wrote:
 
 How to code typesafe multi parameter functions, optional parameters,
 positional parameters, default valued parameters and the like, using the
 the syntax extension I just suggested.

 [ stuff ]

Maybe we can get Walter to incorporate these changes. -- andy

Wait I replied to the wrong post. I meant the environment thing. Disregard! -- andy
Mar 31 2004