Functional programming features

Started by
11 comments, last by OvermindDL1 17 years, 9 months ago
yeah, even if i like function class objects, i don't think they will be in "scope" with the angelscript language. from a scripter point of view, that should help lazy coders that don't understand pointers math, but from my point would turn angelscript in something too similar to how ecmascript behave. the same thing can be achieved in a lot of ways, also with single inheritance:

// this could be even hidden to the script by registering// it on the c++ sideinterface SortClass{  bool compareBasic (int a, int b);}void qsort(List, SortClass sort) { ... }// now this is what u would write in the scriptclass MySort : SortClass{  bool compareBasic (int a, int b) { ... }}void myFunction(){    List l = { ... };    qsort(l, MySort());}

Advertisement
Quote:Yeah, the return type would be the gotcha when it comes to functions as objects in angelscript.


I'm working on fixing that. The calling code will still have to receive the return value in an Any, but the called function should be able to return anything (except types declared in the script...)
Why not specify the return type and all as you do in C++0x (the next version of C++, should be out in a year or two)?

#import <stdio>#import <function>#import <bind>using namespace std;int someFunc(float f):{	return (int)f;}int anotherFunc(float f):{	return (int)(f*2);}struct someStruct{	int i;	someStruct(void) {i=5;}	int structFunc(float f) { return (int)(f*i); }	int operator()(float f) { return (int)(f*(i+2)); }; }int outerStructFunc(someStruct *s, float f) { return (int)(f*(s->i)+1); }int main(int argc, char *argv[]){	function<int(float)> myFunc;	myFunc = &someFunc;	cout << myFunc(2.4) << endl; // prints "2\n"	myFunc = &anotherFunc;	cout << myFunc(2.4) << endl; // prints "4\n"	function<int(someStruct*,float)> sFunc;	someStruct s();	sFunc = &someStruct::structFunc;	cout << sFunc(&s,2.4) << endl; // prints "12\n"	sFunc = &outerStructFunc;	cout << sFunc(&s,2.4) << endl; // prints "14\n"	myFunc = s; // function object / functor	cout << myFunc(2.4) << endl; // prints "16\n"	myFunc = bind(&someStruct::structFunc, &s, _1);	cout << myFunc(2.4) << endl; // prints "12\n"}// can also typedef it an all as usualtypedef function<int(float)> myFunc_td;


etc...

This topic is closed to new replies.

Advertisement