views:

418

answers:

3

What is the syntax to declare a type for my compare-function generator in code like the following?

var colName:String = ""; // actually assigned in a loop
gc.sortCompareFunction = function() : ??WHAT_GOES_HERE??
{
   var tmp:String = colName;
   return function(a:Object,b:Object):int { return compareGeneral(a,b,tmp); };
}();
+1  A: 

Isn't "Function" a data type?

UltimateBrent
Right you are -- that works. I was expecting to need something more precise like in Haskell or F#, but I see ActionScript is more forgiving.
Eric
+2  A: 

Function

Antti
A: 

In order to understand what the data type is, we must know what the intended outcome of the return is. I need to see the code block for compareGeneral, and I still don't believe this will help. You have two returns withing the same function "gc.sortCompareFunction", I believe this is incorrect as return gets a value and then acts as a break command meaning the rest of the anything withing the same function block is ignored. The problem is that I don't know which return is the intended return, and I don't know that flash knows either. You can use * as a data type, but this should only really be used in specific situations. In this situation I believe you need only the one return value that merely returns whatever the value of compareGeneral.

Now if this is a compareGenerator it really should either return a Boolean TRUE or FALSE, or a int 0 or 1, lets use the former. Also I believe we can use one less function. Since I have not seen all of your code and I am not exactly sure what your trying to accomplish, the following is hypothetical.

function compareGeneral(a:object,b:object):Boolean
{
   //Check some property associated to each object for likeness.
   if(a.someAssignedPropery == b.someAssignedPropery)
   {
      return true;
   }
   return false;
}
var objA:Object = new Object();
objA.someAssignedProperty = "AS3";
objB.someAssignedProperty = "AS3";

compareGeneral(objA,objB);

In this case compareGeneral(objA,objB); returns true, though we haven't done anything useful with it yet. Here is a way you may use it. Remember that it either returns a value of true or false so we can treat it like a variable.

if(compareGeneral(objA,objB)) //same as if(compareGeneral(objA,objB)) == true)
{
   trace("You have found a match!");
   //Here you can call some other function or set a variable or whatever you require functionality wise based on a match being found.
}
else
{
  trace("No match could be found!");
}

I hope that this is able to help you understand data types and return values. I do not know what you were doing with tmp, but generally functions that return a value deal with that one thing and only that thing, so it is best that the compare function compare one thing against the other and that be the extent of the call. Whatever functionality you require with tmp can go inside its own function or method, and be called when needed.

For the best AS3 tutorials check out Lee Brimelow at http://www.gotoandlearn.com and http://theflashblog.com.

<3AS3

Brian Hodge