facets

Kevin Bealer Kevin_member at pathlink.com
Tue Feb 28 22:39:07 PST 2006


I like the on_scope_exit() and friends, by the way.

But it got me thinking about an idea I had.  It's a bit like aspect
oriented programming, but that is usually done with classes.  This is
a function based one.  The name I'm thinking of for this is "facet
programming", sort of suggesting "function based aspects".

The idea is to have semi-independent functions that can be "zipped"
together at specified points to combine different implementations of
sub-functionalities, or to add and remove those elements.

Imagine that I have one function that reads lines of data from a file,
another that reads from a user interface, and I want to process the
data one line at a time.  I can also have several "consumers" of the
data.  Here I show two of each.

However, the real power would be in writing a conceptual "script" for
some sequence of interactions (this is represented as "A: B: C:"
below).  This script would allow dozens or hundreds of classes to
interact at controlled places.  Currently this requires writing a
master function that knows everything about each of the interactions
between the parts, for all the parts.

The word "facet" defines a facet of the program; "together" indicates
points that all functions need to reach before any can proceed.  The
modifier "other" means "find this variable name in one of the other
aspects instead of locally".  This is sort of like "extern" in C.

: facet void read_from_file(char[] fname)
: {
:    together A:
:       LineReader lr = new LineReader(name);
:    
:    bool done = lr.eof();
:
:    while(! done) {
:       char[] line = lr.readLine();
:       done = lr.eof();
:    together B:
:    }
:   
:    together C:
:       lr.close();
: }

: facet void parse_lines(char[][] lines)
: {
:    together A:
:
:    bool done = ! lines.length;
:
:    while(! done) {
:       char[] line = lines[0];
:       lines = lines[1..$];
:       done = ! lines.length;
:    together B:
:    }
:
:    together C:
: }

: void interact_print()
: {
:    together A:
:
:    while(1) {
:      together B:
:        if (other done)
:            break;
:
:        writefln("%s", other line);
:    }
:
:    together C:
: }

: int sort_and_unique()
: {
:    together A:
:    
:    int i = 0;
:    bool[char[]] uniquify;
:
:    while(1) {
:      together B:
:        if (other done)
:          break;
:        
:        if (! uniquify.find(other line)) {
:            writefln("%s", other line);
:            uniquify[other line] = true;
:        }
:    }
:
:    int count;
:
:    together C:
:      foreach(char[] line; uniquify.keys) {
:        writefln("%s", line);
:        count++;
:      }
:
:    return count;
: }

Some sort of controlling function would be needed to "zip" together
the facet functions, providing data for their constructors.

: void readAndUnique()
: {
:
:   int count1;
:
:   bind_facets {
:     // runs the following facets simultaneously
:     // nothing can be here except calls to facets
:
:     read_from_file("a.txt");
:     count1 = sort_and_unique();
:   }
:
:   writefln("%d unique lines", count1);
: }

The primary goal here is to allow users to "zip in" locking
(i.e. semaphores), database access, widget set calls, etc.

One other feature: for maximum control, the "together" statement
should take an optional number:

together(140) C:

The facets with the lowest numbers would be executed first after the
number is hit.  The facets would all run in the same thread, but the
compiler is free to mix, reorder, etc. code from different facets as
long as they all stop and wait for each other to get to the same
"together" label.

Facet "binding", i.e. construction of the functions would probably
have to happen at compile time like a template or mixin instantiation.

Kevin





More information about the Digitalmars-d mailing list