In flx_socket.flx, RF notes:

fun str_addr: string -> address = "(void*)$1.c_str()";

proc write_string(strm: flx_socket, s: string)
{
// I have to write a copy, else I get bad/stale memory for the string.
// is this something to do with inlining?
var bugger = s;
// print "writing "; print bugger; endl;
  var slen = len (bugger);
  var addr = str_addr(bugger);  // hop

so I thought I'd explain.

The parameter s is a val, and so represents a first class value.

The function 'str_addr' above is actually illegal.
The reason is it doesn't return a property of the value
of a string .. it returns a property of the object, namely
the address of the internal buffer.

To be correct the function should read:

fun str_addr: lvalue[string] -> address = "(void*)$1.c_str()";

to force the argument to be a variable. OTOH 'len' is
a property of the string's value.

Because Felix substitutes values, you can get this
with the incorrect function:

val s = "Hello";
val a1 = str_addr s;
val a2 = str_addr s;

and now perhaps a1 != a2 because Felix can and at the
moment probably will compile this as:

val a1 = str_addr "Hello";
val a2 = str_addr "Hello";

eliminating s by replacing it with its value.

At the moment, this is not done for variables ..
but this may change in the future when the optimiser
learns how to do data flow analysis. So at the moment:

var s = "Hello";
val a1 = str_addr s;
val a2 = str_addr s;

assures us a1 == a2 .. because the optimiser is too dumb
to realise that s must be "Hello" because it started out
as "Hello" and hasn't been modified.

Using the lvalue[string] argument should defeat even
data flow analysis based substitution, because it is
really passing a pointer to an object, masquerading
as an lvalue. (C is a stupid language .. :)

In principle, string should be immutable .. and there
should be a distinct string_buffer (as in Java).

In any case be aware that returning the pointer to
the internal buffer is more or less equivalent to
a mutator in that it is an *object* attribute:
value are intrinsically immutable, variables are
generally mutable objects.

-- 
John Skaller <skaller at users dot sf dot net>
Felix, successor to C++: http://felix.sf.net



-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to