On Fri, 2006-10-06 at 02:17 +1000, skaller wrote:

> There is no 'new' operator :) I think I had better add one!
> [That's a serious bug .. but as usual a use case is required,
> looks like this is one :]


BTW: the library does contain an xnew[T] function.
[Woops .. changed that to a gen .. :]

  // this hackery is here to force Felix to make 
  // a suitable shape object
  private union _dummy[t] = | _new of t;
  private gen _udata[t]: _dummy[t]->gcptr[t] = "(?1*)$1.data";
  gen xnew[t](x:t) => _udata$ _new x;

This uses a trick -- which means it will fail down the track.
The union variant constructor _new creates a _dummy[t] object, 
which is a

        struct _uctor_ { int variant; void *data; };

where data points to a heap allocated copy of x,
then returns data, cast to t. The type is gcptr[t],
which is a C pointer, not a Felix pointer, but it is one
the type system knows the garbage collector has to track.

This will fail down the track because I will eventually optimise
unions with a small number of variants to a single pointer,
with the variant code stored in the low bits of the pointer:
in this case there's only ONE, so NO bits are required :)

In any case the result type is a pain: you can dereference it
and (I hope) use .-> notation, but you can't pass it where
a Felix pointer is required without calling:

  fun mkref[t]: gcptr[t]->&t = 
    "(#0 const&)flx::rtl::_ref_(0,(void*)$1-NULL)"
  ;

(which i just added to the library .. there was a mkref for ptr[t]
but not gcptr[t] .. :) This operation is safe but it's a pain
and will also probably break down the track.

So this function (generator actually) would define new:

        gen mknew[t](x:t):&t => mkref$ xnew x;

['new' is a keyword at the moment .. :]

It is, however, very ugly (expand the above to see ..)

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


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Felix-language mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to