Version française
Home     About     Download     Resources     Contact us    

This site is updated infrequently. For up-to-date information, please visit the new OCaml website at

Browse thread
[Caml-list] Dynamically evaluating OCaml code
[ Home ] [ Index: by date | by threads ]
[ Search: ]

[ Message by date: previous | next ] [ Message in thread: previous | next ] [ Thread: previous | next ]
Date: 2004-04-08 (00:17)
From: Jon Harrop <jdh30@c...>
Subject: Re: [Caml-list] Dynamically evaluating OCaml code
On Wednesday 07 April 2004 7:47 pm, John Goerzen wrote:
> I am moving from Python to OCaml and one of the things I miss is
> Python's eval() call.  It takes a string representing a bit of Python
> source code, evaluates it, and returns the result.  I would like to be
> able to do similar things with OCaml.

I know exactly what you mean. I used to use an equivalent "EVAL" function in 

I notice that some of the responses to your post stated that this would break 
the type system. I think that this is incorrect because it is failing to 
distinguish between "eval" and dynamic code loading. Obviously we want the 
latter to be typesafe, but the former blurs the distinction between compile- 
and run-time, and valid results of calling "eval" would be a syntax error in 
the given code or, equally, a type mismatch in the result. I suspect you want 
a function which returns one of a set of possible values, or raises 
exceptions in the case of invalid code or return type.

Clement Capel's response is the most interesting so far (IMHO), but I would be 
interested to see a safe version which allowed you to get at the resulting 
data. :-)

Just out of curiosity, what kind of stuff do you want to evaluate? Simple 
mathematical expressions, or actual programs?


To unsubscribe, mail Archives:
Bug reports: FAQ:
Beginner's list: