Dynamic functions - creating functions at runtime without so many ifs/elses

Hi, nice to register after lurking for a while and seeing how smart people are on here…

I am writing some code that needs to be very efficient, yet flexible. This is in a class and when the class gets initialized a function is setup. I want this function to run straight through without doing any time-consuming things such as calling other functions or routines, or doing any if/else logic/branching.

Now I know that you can setup a function at anytime during runtime such as:

var myFunction:Function = function() {trace('HELLO!')}

But what I am trying to do is to try and append code to a function based on logic, yet I don’t want the function to be deciding this logic when it is running as it will only need deciding when the function is created… imagine if it were a string it would be something like myFunction+=“do somethingelse;” which would be the same as having initially setup myFunction as such:

var myFunction = function() {do something; do somethingelse;}

With this I seem to be having no luck :frowning:

Here’s an example where I setup the most compact/efficient function at runtime depending on what I want the function to do. Now this was fairly simple because when we create the function there are only a few possible variations, but I need someway of doing a function+= as my real code has quite a few variations and it would be a nightmare have a zillion if/else branches. REMEMBER I do not want any logic/decisions in the function itself as it to be the most efficient the function must be as compact as possible and run straight-through.


 var dynamicFunction:Function;
 //
 // Setup the dynamic function & call it
 // In this example outputs "Hello Jim" & "Goodbye Jim"
 dynamicFunction = CrapSetupRoutine(true, true, "Jim");
 dynamicFunction();
 //
 // Try and setup the dynamic function using a less ify/elsey (less complicated) routine & call it
 // In this example it doesn't work if we try and make it say both Hello & Goodbye like above :(
 dynamicFunction = SetupRoutineIWouldLike(true, true, "Jim");
 dynamicFunction();
 //
 function CrapSetupRoutine(sayHello:Boolean, sayGoodbye:Boolean, yourName:String):Function {
 	var returnFunction:Function;
	// Far too many ifs/elses...but remember the function created cannot have any if/else logic or calls to other functions
 	if (!sayHello and !sayGoodbye) {
 		returnFunction = function () {
 			trace("<dynamicFunction> sits bored");
 		};
 	} else if (sayHello and sayGoodbye) {
 		returnFunction = function () {
 			trace("Hello "+yourName);
 			trace("Goodbye "+yourName);
 		};
 	} else {
 		if (sayHello) {
 			returnFunction = function () {
 				trace("Hello "+yourName);
 			};
 		} else {
 			returnFunction = function () {
 				trace("Goodbye "+yourName);
 			};
 		}
 	}
 	return returnFunction;
 }
 //
 //What I actually want the Setup function to resemble is this
 function SetupRoutineIWouldLike(sayHello:Boolean, sayGoodbye:Boolean, yourName:String):Function {
 	var returnFunction:Function;
 	// the ifs/elses are as minimal as possible, which is good
 	if (!sayHello and !sayGoodbye) {
 		returnFunction = function () {
 			trace("<dynamicFunction> sits bored");
 		};
 	} else {
 		if (sayHello) {
 			returnFunction = function () {
 				trace("Hello "+yourName);
 			};
 		}
 		if (sayGoodbye) {
			// the += below doesn't work.....so the question is how to append to it?????
 			returnFunction += function () {
 				trace("Goodbye "+yourName);
 			};
 		}
 	}
 	return returnFunction;
 }
 // The end
 

P.S. I knew the += would never work as this is a function and not a string. But at least if you think of the function as a string you’ll get what I’m trying to do…I suspect if it can be done it involves casting or something and maybe using strings initially??