Meta Methods
============

Content published at

http://blogs.codehaus.org/people/bamboo/archives/001593_boo_meta_methods.html
http://blogs.codehaus.org/people/bamboo/archives/001594_boo_meta_methods_ii.html


Context specific quasi-quotation
--------------------------------
	
It is still possible to specify the exact context of quasi-quotation by using
special delimiters for:

 * Expressions - [e| a as int |] # try cast expression
	
 * Statements - [s| a as int |] # variable declaration
 
 * Type Members - [t| a as int |] # field declaration
 
 * Parameters -	[p| a as int |] # parameter declaration
 
 * Modules - [m|
				namespace MyNamespace
		
				import System.Console
		
				WriteLine "hello"
			|] 
			
 * Attributes - [a| [property(Name)] |] # attribute
	

=============================================
BEGIN GRAY AREA
=============================================

It's an arguable point that a more keyword oriented approach to quasi-quotation
would be more in boo's spirit. This should be achievable with a very simple
meta method definition:

	meta def code(tree as Node): 
		// returns a code tree that reconstructs
		// the original tree
		return CodeSerializer().Serialize(tree)
		
Now our 'using' implementation could be rewritten as the more palatable:

	meta def using(e as Expression, block as BlockExpression):
		temp = uniqueName()
		return code:
			$temp = $e
			try:
				$(block.Body)
			ensure:
				if $temp isa IDisposable: ($temp as IDisposable).Dispose()
				
=============================================
END GRAY AREA
=============================================

=============================================
BEGIN HAIRY GRAY AREA
=============================================

Meta extension methods
======================


[extension(IEnumerable)]
meta def each(e as Expression, b as BlockExpression):
"""
Automatically defines the 'it' variable for iterators

Example:
    (1, 2, 3).each:
		print it
"""
	return [|
		for it in $e:
			$(b.Body)
	|]
	


Meta operators
========================

meta def =>(x as ReferenceExpression, y as Expression):
"""
c# lambda syntax operator.
	
	a => b > 2
"""
	return [| { $(x.Name) | return $y } |] 

=============================================
END HAIRY GRAY AREA
=============================================


