Currently thinking about how delegates should work, as I find myself becoming really, really hamstrung without their inclusion in AngelScript. While I think I have some of the implementation worked out, I'm curious what people would want in an implementation and what the syntax should be. From a technical perspective, I think it's best approached at a single-subscriber level, with a delegate storing a function to call and an additional 'this' pointer; multicast delegates/events could be either a library add-on extending the built-in array type or something left to the application interface to provide.

What I'm not sure of, however, is how this should interact with garbage collection (as I understand it, this bites people in the ass with startling frequency in C#, do we need to have a language-level 'weak reference' construct too?) and cross-module function imports. Community, fire away.

clb: At the end of 2012, the positions of jupiter, saturn, mercury, and deimos are aligned so as to cause a denormalized flush-to-zero bug when computing earth's gravitational force, slinging it to the sun.

I feel that delegates is currently one of the most important lacking feature in AngelScript, and one that I was planning to work on for the upcoming releases.

I personally don't have any experience to draw from in designing how delegates should work in AngelScript, so this kind of community input will definitely be important to map out how delegates should work.

Personally I want to make the delegates as simple as possible (e.g. no built-in multicasting). I would also like to see the delegates replace the current function pointers, to reduce the amount of redundant features in the library/language.

I think the C# way is the best approach as far as language features go. Being able to declare a funcdef / delegate type in-place would be a huge help, as would the ability to take a handle to a member function by storing a "this" pointer if necessary.

It could be a problem if the object that owns the method is destroyed before the delegate, though (I think this is what InvalidPointer is talking about with regards to garbage collection). It probably wouldn't be a good idea to have the delegate keep a strong reference to the object to keep it alive. Maybe there could be a way to invalidate the delegate when its object is destroyed, so calling it would throw an "Unbound function" exception just like calling an empty function handle.

I agree with the decision to keep multicasting out of the engine proper. With the addition of a function call operator it would be reasonably simple to create a custom class that acted as a multicast delegate.

I made some delegates system in C++ that I exposed to Angel script in my project with minor modifications of a v2.22.0 WIP of AS (in order to be able to specify a method adress).

This delegate system works with events and subscribers, only subscribers has been exposed to my angelscript (ie AS can catch some Engine event but not raise some event).To expose briefly how it's used:I register a funcdef in the ASEngine which will be my signature (for instance void MouseMoveEvent(CMouse@, const CVec2f&in))Then I expose to the script an "event subscriber" (for instance: CMouseMoveEventHandler) that declares a method "SetCallback" which takes in our exemple (IEventHandler@, MouseMoveEvent@). IEventHandler is an empty interface that I declared so AS classes can register and catch events simply by inherit from it (there may be better solutions, and maybe there already is a hidden common parent class to all script classes)

After that all Game Engine that have events expose a RegisterToEventxxx and a UnregisterFromEventxxx (in our exemple RegisterToEventMouseMove(CMouseEventHandler@+) / UnregisterFromEventMouseMove(CMouseEventHandler@+)

Then the class script declares an EventHandler, implements the "callback" method and all is done(this is pseudo code and may contain syntax errors)

There certainly a better way and a better interface to use the idea to be a little more lightweight for the user, but it might help you to have an idea on how things could be done.

For the delegate implementation in C++, it's an event class that holds a list of generics subscribers using templates (because the system is primarily used with C++). Subscribers are only a "this" pointer and method pointer OR a function pointer so delegates can be either method members with a this or a static method / a function.

I like the C# syntax too (without the multicasting). I think it is the most clean syntax for delegates.

Quittouff:

I'm not sure what customizations you made to the library, but it looks like you don't have a compile time verification of the method signature. Is that right?

There is no common parent class for all script classes, and I do not plan to add one. However, the CScriptHandle add-on would probably work for you as it serves as a generic handle that can hold a reference to any reference object, including script classes.

The only modifications I made is being able to point a method address: I added a "IsSignatureExceptNameAndObjectEqual" that is the same as "IsSignatureExceptNameEqual" and does not care about the object type, so a funcdef is not dependent on the object type but only on the method signature. It may not be ideal but it was the fastest way for me to do what I wanted.

And actually yes, this method provide a compile time verification of the callback signature, if you implement a callback with the wrong signature, it will not compile at the "SetCallback" as the second argument is a pointer to a funcdef. It would produce this kind of error: (example taken for my script sorry if names are not very relevant)

Delegates would need something similar to work, as they are supposed to be able to take either a global function or an object method. I think the opCall feature that cellulose is working on will be needed too.

It's tricky, but can be done similarly to how the property accessors are evaluated already, i.e. the decision to use either set or get needs to be deferred until the very last moment when it can be determined which function signature the delegate is expecting.

Delegates are confirmed. However, I have no plans on making them as powerful as in C# with multicasting and the likes. AngelScript's delegates will be pretty much the existing funcdef & function handle feature, with the additional support to take a handle to a class method with its corresponding object pointer. Similar to the example you posted.

Multicasting and other features of more advanced languages can be added through add-ons if someone should feel they are necessary.

I'll also add that I haven't quite decided if I'll call the feature delegates, or if I'll simply keep using funcdef.

It's difficult to make weak references work safely without putting the responsibility in the hands of the script writer. Unless I can figure out a proper solution, weak references are not going to happen.

At least to begin with, the delegate (or function handle) will hold a strong reference to the object. The GC will deal with potential circular references that arises.

Above code puts to forces script writer to release reference by hand. If she forgets it will be hell to debug why object aren't released.But a weak reference releases object and shows a null pointer access warning, which is much easier to spot.

That is not to say all function pointers should be weak references, strong references also have their places.Maybe a new keyword before objects and function pointers to make them weak pointers.

weak Object @o = Object();

Probably implementation will be a proxy handle.

I am not experienced with GC languages, probably extending discussion out of my ignorance.

You're absolutely correct on the benefits on weak references. However, you're only thinking on how they would be used, not how they have to be implemented in the AngelScript engine

For the weak reference to become null, so it can give the null pointer exception, it means that the object needs to keep track of all weak references that refer to it, so they can be nulled whenever the object is destroyed. This is not easy to implement in a safe manner, much less with little overhead.

two improvements would be- use array to lessen cache misses. WeakHandle needs to hold another integer to do that- weak references hold count so that GC can clean handles array from nulls. Prevent handles array to keep much unnecessary memory.- promote script objects to weak objects only when they are referenced from weak pointers. so we don't query handles array for every destructor call.

I have used this system to catch nasty memory leaks, it works wonderfully.Weak references are much easier to implement than strong ones. That is if the existing system permit this kind of flexibility.

I am not trying to push a new feature on you. Just some ideas for the future of this advanced language.There is nothing like angelscript on the market.

I appreciate the suggestions and I'll keep them for later, but it is not quite as simple as you think.

In C++ you can do it like this because you don't have to bother with the co-existance of weak references and strong references. In AngelScript this would just be a tiny part of the solution required to support weak references. I would also not want to incurr the overhead of keeping track of references to all objects, when most of them are not going to have any weak references anyway.

As I said earlier. If I can think of a proper solution I will definitely add it, as I fully agree that they would provide an important benefit to the library.

I have a reference counting class that mixes weak and strong references without having to track references to all objects. I use a form of intrusive reference counting so it may not apply directly to your needs.

Anyway, the basic idea is to hold the reference counting info in a separate object that sticks around after the object is deleted if and only if the object still has weak references associated with it. Weak_ptrs hold a pointer to the reference counting object so they can check to see if the object has been deleted when access to the object is attempted.

My game objects are split in two. There is CGameObject that implements all the logic, and there is the CGameObjectLink that provides the weak reference. Each CGameObject has a pointer to the corresponding CGameObjectLink, and whenever the CGameObject needs to be destroyed before all references to it have been removed it simply removes the connection to the the CGameObjectLink. The CGameObjectLink object lives on until all references have been removed, but will no longer forward any calls to CGameObject that has been destroyed.

Making this work generically and as transparently as possible in the script language is not easy though, especially when I do not want to incurr a penalty where weak references are not used.