Composite predicates

T

Thong Nguyen

I wrote my own Predicate class for .NET 1.1 which allowed composite
predicates using operator overloading...

for example:

Predicate p1 = {...};
Predicate p2 = {...};

Predicate p1andp2 = ~(p1 ^ p2); // ^ is the AND operator in logic notation

which is requivalent to:

Predicate p1andp2 = delegate(object o) { return !(p1(o) && p2(o)); };

Predicates in .NET/C# 2.0 are delegates, not a class like my implementation
so it's not possible (using C#) to add overloaded operators to the Predicate
delegate. There is no such limitation in the runtime or IL.

What I would like to see is Microsoft add logical compositing operators for
the Predicate delegate in Whidbey. Predicates have a potential to be used
pretty much everywhere where a boolean decision needs to be made (for
flexibility reasons, it can/should replace bool in many areas of the BCL)
and being able to combine many distinct predicates into a single predicate
with an easy to use syntax would be nice....

^Tum
 
D

Daniel O'Connell [C# MVP]

Thong Nguyen said:
I wrote my own Predicate class for .NET 1.1 which allowed composite
predicates using operator overloading...

for example:

Predicate p1 = {...};
Predicate p2 = {...};

Predicate p1andp2 = ~(p1 ^ p2); // ^ is the AND operator in logic notation

which is requivalent to:

Predicate p1andp2 = delegate(object o) { return !(p1(o) && p2(o)); };
What I would like to see is Microsoft add logical compositing operators
for the Predicate delegate in Whidbey. Predicates have a potential to be
used pretty much everywhere where a boolean decision needs to be made (for
flexibility reasons, it can/should replace bool in many areas of the BCL)
and being able to combine many distinct predicates into a single predicate
with an easy to use syntax would be nice....

I think you could easily just provide a method to achieve this(using dynamic
methods). While compositing has some interesting concepts, I'm not sure its
really worth *THAT* much as to change the language for one specific case.

Also, even though ^ may be and in logic notation, using it in C# is a
mistake, IMHO. You'll confuse far more people by using an operator the
language already has noted as xor than you will by using one the logic does
not use.

It is an interesting concept, however.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top