> Let's say we have an NSArray containing "person" objects structured as
> follows:
> 
> {firstName:String, lastName:String, age:Number, canDrive:Boolean}
> 
> Now, let's say in your application, you allow the users to filter by all
> sorts of criteria. Suppose you wanted to display all drivers over 60.
> The predicate string to describe this criteria would be as follows:
> 
> "canDrive == true AND age >= 60"
> 
> To actually filter the array, here's what you'd do:
> 
> var pred:NSPredicate = NSPredicate.predicateWithFormat("canDrive == true
> AND age >= 60"); var driversOver60:NSArray =
> people.filteredArrayUsingPredicate(pred);
> 
> And that's it. Pretty cool, huh?

Yes, but it's fully untyped.

What if you make a typo "candrive" instead of "canDrive" or mistake the 
flag (using "cantDrive"). What if you apply the predicate to the wrong 
Array ? what if you rename one of your objet fields ? All of theses will 
be runtime errors. There are not so bad, but if they happen late in the 
development of your application, they can make you loose significant 
time debugging it.

In haXe for instance, you could be able to fully type it :

class Filter<T> {

        static function filterArray(
                pred : T -> Bool,
                arr : Array<T>
        ) : Array<T> {
                var arr2 = new Array();
                var i = 0;
                var l = arr.length;
                while( i < l ) {
                        var elt = arr[i];
                        if( pred(elt) )
                                arr2.push(elt);
                        i++;
                }
                return arr2;
        }

}


Then you can write your predicate like this :

var pred = function(p : NSPerson) { return p.canDrive && p.age >= 60; };

Also, you're not restricted to some syntax, since you can use the whole 
language as syntax, and even call methods :

var pred = function(p : NSPerson) { return p.birthday() == today; };

You could do the same with MTASC, but with less type checks.
My two eurocents ;)

Nicolas

_______________________________________________
osflash mailing list
[email protected]
http://osflash.org/mailman/listinfo/osflash_osflash.org

Reply via email to