Using enums?

Feb 7, 2008 at 9:13 PM
Edited Feb 7, 2008 at 10:15 PM
First, this is a great library! I was wondering if it was possible to use enum types in an expression evaluation. I can't seem to find a precedent/example for this. I'm using Flee 0.9.16.0. I'm trying to do something like:

OrderAcceptExpressionOwner owner = new OrderAcceptExpressionOwner();
ExpressionContext context = new ExpressionContext(owner);
// add enum types
context.Imports.AddType(typeof(PaymentTypeEnum));
// variable creation omitted...
// assume availability of a variable x of type PaymentTypeEnum

// evaluate individual rule
IGenericExpression<bool> e = ExpressionFactory.CreateGeneric<bool>("x = PaymentTypeEnum.CreditCard");
bool evalresult = e.Evaluate();


PaymentTypeEnum is not recognized in the above expression. I think I could get this to work using some form of dynamic variable evaluation, or by doing something creative within the expression owner to simulate returning a variable with the name PaymentTypeEnum and properties representing the enum values, but was hoping there was a more straightforward approach that I was missing.

Thanks in advance!

Eric
Feb 7, 2008 at 10:14 PM
Edited Feb 7, 2008 at 10:15 PM
Aha! I closer reading of the error messages suggested the following, which seems to work:

OrderAcceptExpressionOwner owner = new OrderAcceptExpressionOwner();
ExpressionContext context = new ExpressionContext(owner);
// add enum types
context.Imports.AddType(typeof(PaymentTypeEnum), "PaymentTypeEnum");
// variable creation omitted...
// assume availability of a variable x of type PaymentTypeEnum

// evaluate individual rule
IGenericExpression<bool> e = ExpressionFactory.CreateGeneric<bool>("Cast(x, int) = Cast(PaymentTypeEnum.CreditCard, int)");
bool evalresult = e.Evaluate();


Based on the return messages, it looks like the primary problem wasn't handling enums, it was more in a) importing it with a namespace so it could be referenced with that name, and b) casting both values to something that Flee could do an equality test on. If there is a better recommended approach, I'd love to hear it, though!

Eric
Coordinator
Feb 8, 2008 at 1:32 AM
Actually, in this case it seems you found a bug. Comparing an enum against an integer requires a cast but comparing two enums (of the same type) should work.

I've added this as an issue to be fixed in the next release.
Coordinator
Feb 21, 2008 at 2:21 AM
In Flee-0.9.17.1, you should now be able to compare two variables of the same enum without requiring a cast.