Function call operators in the future?

Started by
16 comments, last by cellulose 11 years, 7 months ago
I'm curious about future prospects on a function call operator in Angelscript. Messy though they can be, operator() overloads are super-useful and something I use frequently in C++ to avoid wordy APIs. It also allows for functors and the like.

As an example, I have a module called "Random" in my game engine, exposed to Angelscript as a global, which can generally be treated like an overloaded function to generate random numbers with various parameters. Additional Random objects can also be created for deterministic simulations and the like, so I'd be losing some functionality if I just exposed this stuff as global functions.


class Random
{
public:
Uint32 operator()(Uint32 max);
Uint32 operator()(Uint32 min, Uint32 max);

Sint32 operator()(Sint32 max);
Sint32 operator()(Sint32 min, Sint32 max);

float operator()(float max);
float operator()(float min, float max);

double operator()(double max);
double operator()(double min, double max);

// Normal distribution
float normal(float mean, float deviation);

// Exponential normal distribution
float normalExp(float mean, float multDeviation);


// ...
};


Is this something that is likely to be added to Angelscript in the future, or would it throw a wrench in the design?
Advertisement
I currently do not have any plans on adding support for function call operators in the script language. Though I won't go so far as saying that it will never be done, I just don't feel they add enough value to make it worth it at the moment.

That doesn't mean you can't register your Random class with AngelScript though. You just have to give a name for your nameless functions when registering them, example:


engine->RegisterObjectMethod("Random", "uint32 max(uint32)", asMETHODPR(Random, operator(), (UInt32), UInt32), asCALL_THISCALL);
engine->RegisterObjectMethod("Random", "uint32 between(uint32, uint32)", asMETHODPR(Random, operator(), (UInt32, UInt32), UInt32), asCALL_THISCALL);

engine->RegisterObjectMethod("Random", "int32 max(int32)", asMETHODPR(Random, operator(), (SInt32), SInt32), asCALL_THISCALL);
engine->RegisterObjectMethod("Random", "int32 between(int32, int32)", asMETHODPR(Random, operator(), (SInt32, SInt32), SInt32), asCALL_THISCALL);
engine->RegisterObjectMethod("Random", "float max(float)", asMETHODPR(Random, operator(), (float), float), asCALL_THISCALL);
engine->RegisterObjectMethod("Random", "float between(float, float)", asMETHODPR(Random, operator(), (float, float), float), asCALL_THISCALL);


Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

For my part, it's a feature I'd really like to see, but I've also written a parser or two and know it would be a giant pain in the butt to implement. So I understand if it's late or never. To be honest I'd take property accessors over function-call operators any day of the week.

I'll keep my fingers crossed nonetheless. :)

To be honest I'd take property accessors over function-call operators any day of the week.


Property accessors are already supported.

Or perhaps you had something different in mind?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Oh, no, I'm quite aware. I'm saying I'm happy to have property accessors. But anyway. Keeping my fingers crossed. And who knows? Maybe if I'm antsy enough it's the sort of thing I could write a preprocessor for. :)
Pardon the necro-post; the release of version 2.24 makes this relevant again.

A statement from Andreas in my topic on hardcoding geometry and initializer lists:

TheAtom reported a bug today about making calls with function defs. I still need to investigate what changes needs to be made to support those scenarios, but as they look similar in syntax to what the use of a function call operator might look like I recommend you wait a little until I've fixed that bug before you dig too deep into the code.[/quote]

Since the improved semantics for function pointers are now implemented, how straightforward would the implementation of an opCall be? (Either as a new official addition to the language or, barring that, as a hack to my own copy?)
I believe it ought to be pretty straight forward now. It should only require a few changes to the asCCompiler class. There are basically two places that needs to be updated, CompileFunctionCall() and CompileExpressionPostOp().

The first is for when the expression looks like an ordinary function call, i.e.


Object obj;
obj(); // <-- CompileFunctionCall() needs to look for an opCall method in the Object class


The second is for when the expression preceeding the argument list isn't a simple symbol, but in itself another expression that returns an object that implements an opCall method, e.g.


array<Object> arr;
arr[0](); // <-- CompileExpressionPostOp() needs to look for an opCall method in the Object class


The easiest way to determine what to change is by debugging the compiler while compiling a script you want to work.


While I'm not a big fan of call operators myself, I can still incorporate them into the language as they don't cause any negative impacts.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

In that case I eagerly await the next release. :D

While I'm not a big fan of call operators myself, I can still incorporate them into the language as they don't cause any negative impacts.


I do not want to disappoint you but I didn't say I would implement this. ;)

I meant that if someone were to implement it and send me a patch I would incorporate it into the library. Why don't you give it a try yourself?

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Already at work on it.

Are there any particular caveats responsible for this remark in CompileFunctionCall? (Version 2.24.1)

// TODO: funcdef: It is still possible that there is a global variable of a function type


Or would it be safe to add the variable-access resolution code?

EDIT: Ah, it looks like it's superfluous...

This topic is closed to new replies.

Advertisement