[Mono-list] Implementing attribute-based code generation

Rodrigo B. de Oliveira rbo@acm.org
Thu, 13 Jan 2005 13:29:06 -0200


So, yes, you are talking about syntactic attributes. :)

And you are right the website does not make them look any good.

>From the boo manifesto [1]:

Syntactic Attributes
====================
Very few languages have a good way of automating micro code patterns, those
little lines of code we programmers are generally required to write in order
to expose fields as properties, properly validate method arguments, check
pre/post conditions among other things. Enter syntactic attributes:

class Person:
	[getter(FirstName)]
	_fname as string
	
	[getter(LastName)]
	_lname as string
	
	def constructor([required] fname, [required] lname):
		_fname = fname
		_lname = lname

Look carefully at the code above, although getter and required look just
like regular .net custom attributes they would be recognized by the compiler
as syntactic attributes - attribute classes that implement the IAstAttribute
interface. The compiler will treat such attributes in a very special way, it
will give them a chance to transform the in memory code representation (the
abstract syntax tree, ast for short). After this process the code tree would
look as if the programmer had actually typed:

class Person:
	
	_fname as string
	
	_lname as string
	
	def constructor(fname, lname):
		raise ArgumentNullException("fname") if fname is null
		raise ArgumentNullException("lname") if lname is null
		_fname = fname
		_lname = lname
		
	FirstName as string:
		get:
			return _fname
			
	LastName as string:
		get:
			return _lname

Anyone can write syntactic attributes just as anyone can write classes that
implement interfaces. This opens up for entirely new ways of expression and
code composition.

Best wishes,
Rodrigo

[1] http://boo.codehaus.org/BooManifesto.pdf

> -----Original Message-----
> From: mono-list-admin@lists.ximian.com 
> [mailto:mono-list-admin@lists.ximian.com] On Behalf Of Stephen Touset
> Sent: Thursday, January 13, 2005 1:36 AM
> To: Rodrigo B. de Oliveira
> Cc: mono-list@lists.ximian.com
> Subject: Re: [Mono-list] Implementing attribute-based code generation
> 
> Rodrigo B. de Oliveira wrote:
> 
> >It sounds like you are talking about what we call syntactic 
> attributes.
> >  
> >
> 
> I couldn't tell from the website. Essentially, I am trying to 
> add design by contract support to the mono C# compiler (as an 
> extension, rather than a core patch to mcs). The best way to 
> do this would seem to be to have attributes that can modify 
> methods. In other words:
> 
> [Precondition("o != null")]
> [Postcondition("size > 0")]
> void AddObject(Object o) {
>     // ... do stuff ...
>     return;
> }
> 
> Which would then at compile time insert
> 
> void AddObject(Object o) {
>     Check.Require(o != null);
>     // ... do stuf ...
>     Check.Ensure(size > 0);
>     return;
> }
> 
> While also obeying design by contract inheritance rules.
> _______________________________________________
> Mono-list maillist  -  Mono-list@lists.ximian.com 
> http://lists.ximian.com/mailman/listinfo/mono-list
> 
>