[IronPython] Type analysis of expression

Jonathan Hartley tartley at tartley.com
Mon Oct 19 07:18:46 PDT 2009

Hey all,
Stephen, I infer that Christian can't evaluate the expression yet. (If 
he could, then deriving the type of the result would be trivial) 
Presumably the variables in the expression don't yet have known values.

So he has an expression which cannot be evaluated, but he wants to guess 
what type the return value would be if the variables in the expression 
were known.

The variables in the expression do have known types. So I wonder if you 
could pick a prototypical value for each variable, (eg. set all floats 
to 1.0, and all integers to 1) and then evaluate the expression to see 
what type the result is?

Obviously this won't work for all expressions (eg. "x / (x - 1)"), but 
maybe it would work enough of the time, depending on what form your 
expressions take.

Am I misunderstanding entirely, or only partially?


Lepisto, Stephen P wrote:
> What about treating the return type of the python expression as object then coerce the object into the type required by the database?  If the coercion fails, then that could be treated as a type error.
> For example, after using python to evaluate the expression '2+(4*5)', the returned object could be converted to an integer using System.Convert.ToInt32() or to a string with System.Convert.ToString().  System.Convert throws InvalidCastException for those cases that cannot be converted.
> -----Original Message-----
> From: users-bounces at lists.ironpython.com [mailto:users-bounces at lists.ironpython.com] On Behalf Of Michael Foord
> Sent: Monday, October 19, 2009 4:21 AM
> To: Discussion of IronPython
> Subject: Re: [IronPython] Type analysis of expression
> Christian Schmidt wrote:
>> Hello,
>> we are using IronPython embedded into our application to evaluate user
>> defined expression. The "variables" used in the expressions are strongly
>> typed and the results need to be written back into a database.
>> I know that the return type of a dynamic expression cannot be determined
>> in general. But what would be a pragmatic way to guess the return type
>> of an expression?
> Hehe. If you're evaluating arbitrary functions provided by the user then 
> I don't know of any way of 'inferring' the return type - beyond parsing 
> it and modelling the type flow through the expression (which would be a 
> lot of work).
> All the best,
> Michael
>> Thanks,
>> Christian
>> _______________________________________________
>> Users mailing list
>> Users at lists.ironpython.com
>> http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Jonathan Hartley      Made of meat.      http://tartley.com
tartley at tartley.com   +44 7737 062 225   twitter/skype: tartley

More information about the Users mailing list