1.3. Seamless Binding

Felix purports to support a property called seamless binding. What this means is that the boundary between C++ and Felix code is fluid. To illustrate this, lets consider a version of the above code written entirely in Felix.
Start felix section to tut/embedding/bind-1.02-0.flx[1 /1 ]
     1: #line 182 "./lpsrc/flx_tut_bind.pak"
     2: #import <flx.flxh>
     3: struct gauss = {
     4:   x : int;
     5:   y : int;
     6: }
     7: 
     8: proc _set ( lhs: &gauss, rhs: gauss )
     9: {
    10:   (*lhs).x = rhs.x;
    11:   (*lhs).y = rhs.y;
    12: }
    13: 
    14: fun add (a:gauss, b:gauss): gauss = {
    15:   return gauss(a.x+b.x, a.y+b.y);
    16: }
    17: 
    18: fun mul (a:gauss, b:gauss): gauss = {
    19:   return gauss(a.x+b.x - a.y+b.y, a.x*b.y + a.y*b.x);
    20: }
    21: 
    22: fun mkgauss (a:int,b:int):gauss = { return gauss(a,b); }
    23: fun real (z:gauss):int = { return z.x; }
    24: fun imag (z:gauss):int = { return z.y; }
    25: 
    26: proc gprint(z:gauss) {
    27:   print "(";
    28:   print (real z);
    29:   print ", ";
    30:   print (imag z);
    31:   print ")";
    32: }
    33: 
    34: fun sqr(z:gauss):gauss = {
    35:   return z * z;
    36: }
    37: 
    38: fun norm(z:gauss): int = {
    39:   return
    40:     real z * real z + imag z * imag z
    41:   ;
    42: }
    43: 
    44: val z1 = mkgauss(1,2);
    45: val z2 = z1 + z1;
    46: val z3 = sqr z2;
    47: val n = norm z3;
    48: gprint z1; endl;
    49: gprint z2; endl;
    50: gprint z3; endl;
    51: print n; endl;
End felix section to tut/embedding/bind-1.02-0.flx[1]
Start data section to tut/embedding/bind-1.02-0.expect[1 /1 ]
     1: (1, 2)
     2: (2, 4)
     3: (4, 16)
     4: 272
End data section to tut/embedding/bind-1.02-0.expect[1]
The difference between these two programs is that in the second one, gauss is a concrete non-primitive Felix data type. In the first program, gauss is an abstract data type, together with a binding specifying the semantics in C++.

As you can guess, the whole of the program could have been written in C++ rather than Felix.

It's up to you to choose what parts of your Felix program are written directly in C++, and which parts are written in Felix: since Felix is a C++ code generator, it all ends up as C++ anyhow.

Usually, you'll write Felix, except when you have an existing code base containing useful types you need to work with.