#2025 Eval user-provided expression on an object

rhodes Wed 12 Sep 2012

What is the best way to evaluate a user-provided expression in the context of an object's field values?

For example, if the program has a list of Student objects...

class Student
{
  Str name
  Decimal gpa
}

And a user provides a string predicate of fantom code...

gpa > 3.0

What is the best way for me to filter the list using the string predicate they provide?

Str predicate := "gpa > 3.0"
Student[] students := ...
students.findAll ????

brian Wed 12 Sep 2012

Fantom doesn't really have an "eval". If you construct a fully correct source of an entire class you can compile into a script using sys::Env.compileScript. But I personally would write my own little parser for something like that (but then again I'm a compiler writer :-)

mike Thu 13 Sep 2012

Busted link to Env -- here's the link:

http://fantom.org/doc/sys/Env.html#compileScript

Login or Signup to reply.