facets

Kevin Bealer Kevin_member at pathlink.com
Wed Mar 1 08:33:11 PST 2006


In article <du4f5r$t0d$1 at digitaldaemon.com>, Ben Phillips says...
>
>This idea is interesting, but also confusing. From what I can tell using
>"together" means that the facets 
>don't execute linearly, since they depend on each other. Imho this would be very
>confusing to code and 
>even more confusing to understand. I even have trouble understanding your
>example, I can get the 
>basics but its confusing how the scopes of all the facets seem to be linked
>through the use of "other". 
>This leads me to believe that facets would also be very difficult to implement
>in a compiler.

Here is what I'm thinking on how to implement it, using a simpler example:

: facet void compute_n()
: {
:    int n;
:  together C:
:    writefln("%d", n);
: }

: facet void write_x()
: {
:       writefln("x");
:    together A:
:       n += 3;
:       writefln("x");
:    together B:
:       n += 5;
:       writefln("x");
:    together C:
: }

: facet void write_y()
: {
:       writefln("y");
:    together A:
:       n ++;
:       writefln("y");
:    together B:
:       n += 2;
:       writefln("y");
:    together C:
: }

: void write_x_and_y()
: {
:   bind_facets {
:     write_x();
:     write_y();
:     compute_n();
:   }
: }

The compiler would read all this in, and produce a function like this:

: void write_x_and_y()
: {
:    int n;
:    writefln("x");
:    writefln("y");
:
:  //together A:
:    n += 3;
:    writefln("x");
:    n ++;
:    writefln("y");
:
:  //together B:
:    n += 5;
:    writefln("x");
:    n += 2;
:    writefln("y");
:
:  //together C:
:    writefln("%d", n);
: }

The "together" and "other" directives are compiler hints - at runtime there is
just the one resulting function.  Kinda like a mixin.

The scopes of the facets are effectively seperate, with "other" acting a little
like C language 'extern' - it tells the compiler that its okay to look in other
facets for this variable name.  It is an error to use "other" with a name that
appears in more than one facet (i.e. it must be unambiguous).

Kevin





More information about the Digitalmars-d mailing list