title: Statically Typed Metaprogramming?
body: <p>I've been thinking about what I would miss in porting some Python code to a statically typed language such as F# or Scala; the libraries can be substituted, the conciseness is comparable, but I have lots of python code which is as follows:</p>
  
  <pre><code>@specialclass
  class Thing(object):
      @specialFunc
      def method1(arg1, arg2):
          ...
      @specialFunc
      def method2(arg3, arg4, arg5):
          ...
  </code></pre>
  
  <p>Where the decorators do a huge amount: replacing the methods with callable objects with state, augmenting the class with additional data and properties, etc.. Although Python allows dynamic monkey-patch metaprogramming anywhere, anytime, by anyone, I find that essentially all my metaprogramming is done in a separate "phase" of the program. i.e.:</p>
  
  <pre><code>load/compile .py files
  transform using decorators
  // maybe transform a few more times using decorators
  execute code // no more transformations!
  </code></pre>
  
  <p>These phases are basically completely distinct; I do not run any application level code in the decorators, nor do I perform any ninja replace-class-with-other-class or replace-function-with-other-function in the main application code. Although the "dynamic"ness of the language says I can do so anywhere I want, I never go around replacing functions or redefining classes in the main application code because it gets crazy very quickly. </p>
  
  <p>I am, essentially, performing a single re-compile on the code before i start running it.</p>
  
  <p>The only similar metapogramming i know of in statically typed languages is reflection: i.e. getting functions/classes from strings, invoking methods using argument arrays, etc. However, this basically converts the statically typed language into a dynamically typed language, losing all type safety (correct me if i'm wrong?). Ideally, I think, I would have something like the following:</p>
  
  <pre><code>load/parse application files 
  load/compile transformer
  transform application files using transformer
  compile
  execute code
  </code></pre>
  
  <p>Essentially, you would be augmenting the compilation process with arbitrary code, compiled using the normal compiler, that will perform transformations on the main application code. The point is that it essentially emulates the "load, transform(s), execute" workflow while strictly maintaining type safety.</p>
  
  <p>If the application code are borked the compiler will complain, if the transformer code is borked the compiler will complain, if the transformer code compiles but doesn't do the right thing, either it will crash or the compilation step after will complain that the final types don't add up. In any case, you will never get the runtime type-errors possible by using reflection to do dynamic dispatch: it would all be statically checked at every step.</p>
  
  <p>So my question is, is this possible? Has it already been done in some language or framework which I do not know about? Is it theoretically impossible? I'm not very familiar with compiler or formal language theory, I know it would make the compilation step turing complete and with no guarantee of termination, but it seems to me that this is what I would need to match the sort of convenient code-transformation i get in a dynamic language while maintaining static type checking.</p>
  
  <p>EDIT: One example use case would be a completely generic caching decorator. In python it would be:</p>
  
  <pre><code>cacheDict = {}
  def cache(func):
      @functools.wraps(func)
      def wrapped(*args, **kwargs):
          cachekey = hash((args, kwargs))
          if cachekey not in cacheDict.keys():
              cacheDict[cachekey] = func(*args, **kwargs)
          return cacheDict[cachekey]
      return wrapped
  
  
  @cache
  def expensivepurefunction(arg1, arg2):
      # do stuff
      return result
  </code></pre>
  
  <p>While higher order functions can do some of this or objects-with-functions-inside can do some of this, AFAIK they cannot be generalized to work with any function taking an arbitrary set of parameters and returning an arbitrary type while maintaining type safety. I could do stuff like:</p>
  
  <pre><code>public Thingy wrap(Object O){ //this probably won't compile, but you get the idea
      return (params Object[] args) =&gt; {
          //check cache
          return InvokeWithReflection(O, args)
      }
  }
  </code></pre>
  
  <p>But all the casting completely kills type safety.</p>
  
  <p>EDIT: This is a simple example, where the function signature does not change. Ideally what I am looking for could modify the function signature, changing the input parameters or output type (a.l.a. function composition) while still maintaining type checking.</p>