On 05/03/2012, at 12:57 PM, john skaller wrote:

> Ok, so this now works:
> 
> //////
> struct X {
>  a:int;
>  b:double;
>  fun f (x:int) => self.a + x + x;
>  fun g  => self.a;
>  proc s (x:int) => self.a = x;
> };

So now I am thinking: what if we implemented *real* C++ methods?

It comes close to replacing "self" with "this", and of course
keeping track of the methods as struct members. Felix used to have
classes which did something like this but I chucked them out:
it was a lot of extra complexity and the functions were still implemented
as above (not as C++ methods). The advantage of the hackery above
it that it is done entirely in the parser (using Scheme actions).

However *real* methods open the possibility of tagging them "virtual",
and getting some OO via C++.

There's another, possibly even more important consideration.
It goes like this:

Currently, Felix models functions and procedures each with a specific
kind of C++ class. Procedures, for example, have a resume() method.
Both have clone() methods. The typing is done by inheritance,
so a function type is an abstract class, and a function of that type
is derived from it.

But the modelling is fixed by code in the compiler.

What if the end user could design their own implementation models?
In a normal language this would be useless because all the syntactic
forms would map to hard coded implementations, so there'd be no
way to use the new ones from syntax, you'd have to manually construct
instances with a set of function calls or something.

Of course .. Felix doesn't have this restriction, since the grammar is 
dynamically
loaded.

Ok, so lets look at an example that is, in fact, a current work in progress,
although the progress is very slow: dynamic type construction.

Felix has RTTI objects, which the garbage collector uses to find where pointers
are. Originally this was just a couple of size counters plus an array of 
offsets,
PLUS a slot for a finaliser function, defaulting to the C++ destructor.

Now, Felix has been extended so custom finalisers and pointer scanners
can be written (in C only, mind you, but remember Felix is C :)

It's quite clear (and intended!!) that as with other languages like Python,
the set of functional slots in a type record can be extended to include
clone() functions, and other things which would allow the language
to manipulate objects: copy, assign, clone, serialise, print ...

The real problem with all this is that at the moment, it's just a plain
data structure. It's not hard to generate these: I don't mean just to 
emit C++ text that compiles, I mean *run time* construction
of new types. The scanner and finaliser can't be done in Felix
yet. The types are not composable either: if you have structs,
you'd like to define a struct type by a composition rule and an
array of type objects for the members.

Worse, you can do a scanner for say vector<X>, but you need
another for vector<Y>. The offsets differ, RTTI objects aren't
polymorphic.

There's another way: replace the RTTI data structures with C++
class objects and use virtual function polymorphism.

Which brings us a use case for making structs with methods
which are actually C++ methods, not C like functions that take
an extra argument: the latter can't be dispatched dynamically
without extra machinery.

Ok .. sorry .. end of thinking aloud :)

--
john skaller
skal...@users.sourceforge.net
http://felix-lang.org




------------------------------------------------------------------------------
Keep Your Developer Skills Current with LearnDevNow!
The most comprehensive online learning library for Microsoft developers
is just $99.99! Visual Studio, SharePoint, SQL - plus HTML5, CSS3, MVC3,
Metro Style Apps, more. Free future releases when you subscribe now!
http://p.sf.net/sfu/learndevnow-d2d
_______________________________________________
Felix-language mailing list
Felix-language@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/felix-language

Reply via email to