Is there a term for this concept, and does it exist in a static-typed language?

Posted by Strilanc on Stack Overflow See other posts from Stack Overflow or by Strilanc
Published on 2010-05-13T03:50:22Z Indexed on 2010/05/13 3:54 UTC
Read the original article Hit count: 218

Filed under:
|
|

Recently I started noticing a repetition in some of my code. Of course, once you notice a repetition, it becomes grating. Which is why I'm asking this question.

The idea is this: sometimes you write different versions of the same class: a raw version, a locked version, a read-only facade version, etc. These are common things to do to a class, but the translations are highly mechanical. Surround all the methods with lock acquires/releases, etc. In a dynamic language, you could write a function which did this to an instance of a class (eg. iterate over all the functions, replacing them with a version which acquires/releases a lock.).

I think a good term for what I mean is 'reflected class'. You create a transformation which takes a class, and returns a modified-in-a-desired-way class. Synchronization is the easiest case, but there are others: make a class immutable [wrap methods so they clone, mutate the clone, and include it in the result], make a class readonly [assuming you can identify mutating methods], make a class appear to work with type A instead of type B, etc.

The important part is that, in theory, these transformations make sense at compile-time. Even though an ActorModel<T> has methods which change depending on T, they depend on T in a specific way knowable at compile-time (ActorModel<T> methods would return a future of the original result type).

I'm just wondering if this has been implemented in a language, and what it's called.

© Stack Overflow or respective owner

Related posts about theory

Related posts about types