2. What does meta mean?
Meta (from the Greek preposition and prefix meta-
(μετά-) meaning "after", or "beyond") is a prefix used
in English to indicate a concept which is an
abstraction from another concept, used to complete
or add to the latter.
3. Metaprogramming?
Metaprogramming is the writing of computer
programs with the ability to treat programs as their
data. It means that a program could be designed to
read, generate, analyse and/or transform other
programs, and even modify itself while running.
4. • Imagine a guy who builds cars. Say it's the same
thing as using a computer. At some point he
realizes he's always doing the same thing, more or
less.
• So he builds factories to build cars, and it's much
better. He's now programming ! Nevertheless, once
again, at some point, he realizes he's always doing
the same thing, to some extent.
• Now he decides to build factories that build
factories that build cars. That's metaprogramming.
5. Metaprogramming benefits
• Keeps code small, simple, DRYer, lighter, more
intuitive and more scalable
• Rapid prototyping
• Minimize the LOC to express a solution
• Makes good programmers really productive
7. C Macros
• C constants allow us to fix program behaviour at
development time
• C variables allow us to modify program behaviour
at execution time
• C macros allow us to modify program behaviour at
compilation time
10. C Macros
#include <stdio.h>
#define max_of(x, y) (x > y) ? x : y
int main(int argc, const char * argv[]){
printf("The biggest one of 2 and 5 is %dn", max_of(2, 5)); //prints 5
printf("The biggest one of 3.14 and 6.02 is %.02fn", max_of(3.14, 6.02)); //prints 6.02
printf("The biggest one of 'a' and 'z' is '%c'n", max_of('a', 'z')); //prints 'z'
}
11. Reflection
Ability of a computer program to examine and modify
the structure and behaviour (specifically the values,
meta-data, properties and functions) of the program
at runtime.
12. JAVA Reflection
public class Main {
public void foo(){
System.out.println("I'm foo");
}
public void bar(){
System.out.println("I'm bar");
}
public static void main(String[] args){
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Please enter a method name: ");
try {
String method_name = bf.readLine();
Object obj = Class.forName("com.metaprogramming.Main").newInstance();
Class klass = obj.getClass();
Method method = klass.getMethod(method_name, null);
method.invoke(obj, null);
} catch (Exception e) {
e.printStackTrace();
}
}
}
13. JAVA Reflection
Object obj = Class.forName(“Main").newInstance();
Class klass = obj.getClass();
Method method = klass.getMethod(“foo”, null);
method.invoke(obj, null);
14. AST
In computer science, an abstract syntax tree (AST),
or just syntax tree, is a tree representation of the
abstract syntactic structure of source code written in
a programming language. Each node of the tree
denotes a construct occurring in the source code.
19. Introspection/Reflection:
instance variables
class Foo
def initialize
@bar = 'Bar'
end
end
f = Foo.new
f.instance_variable_get(‘@bar') #'Bar'
f.instance_variable_set(‘@baz', 'Baz')
f.instance_variable_get(‘@baz') #'Baz'
f.instance_variables #[:@bar, :@baz]
20. Introspection/Reflection:
class variables
class Foo
@@bar = 'Bar'
end
Foo.class_variable_get('@@bar') #'Bar'
Foo.class_variable_defined?(‘@@bar') #true
Foo.class_variable_set('@@baz', 'Baz')
Foo.class_variable_get('@@baz') #'Baz'
Foo.class_variables #[:@@bar, :@@baz]
21. Introspection: methods
class Foo
def bar; 'I'm bar' end
private
def baz; 'I'm baz' end
protected
def zoo; 'I'm zoo' end
end
f = Foo.new
f.methods #[:bar, :zoo, ...]
f.private_methods #[:baz, ...]
f.protected_methods #[:zoo, ...]
22. Reflection: methods
class Foo
def bar; 'I'm bar' end
private
def baz; 'I'm baz' end
protected
def zoo; 'I'm zoo' end
end
f = Foo.new
%w(bar baz zoo).each do |method|
f.send(method)
end