[plt-scheme] Re: Novice question: evaluating symbols

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Jan 8 10:34:41 EST 2010

On Jan 8, 2010, at 10:26 AM, Grant Rettke wrote:

> I just meant the part about the "an eval-like thing and should be
> avoided in any well-designed language" part though.
> There are posts on the list and a lot of them include simply "just
> don't do it" but I wonder if there is a single place that captures all
> of the downsides, and when they are worth it.

Think about it.

If you include (eval 'SomeFatS-Expression) in your program (or some  
such form),

  your run-time system must include the entire compiler;
  you can't strip it out.

  your static analysis (and you need one in this day and age
  to compile well) is thrown off; eval is a so-called black
  hole that provides no information (bottom or top, depending
  on which kind of lattice you use to 'hang up' your analysis).

  your own reasoning capabilities are limited about this
  program. You never know what is really in SomeFat ...

So, well-designed programs avoid eval and over five decades of
experience with eval in the Lisp community, we know many techniques
to achieve this.

  -- if it is just a symbol, use a table mapping symbols to
     first-class functions; why else have those?
	(you can even extend this table dynamically)
  -- if you can't use a table, a macro may work
  -- if it is a fixed S-expression without any pieces from
     I/O, use a macro, which is expanded before the analysis
     kicks in
  -- if it really depends on I/O, you need eval. Be careful.
     Re-read the preceding sentence five times. Then proceed.

Good programmers know this. They have been trained in a good PL course. 

Posted on the users mailing list.