With the recent release (and open sourcing) of the Roslyn compiler for the .NET framework, I’ve started thinking about how it might be constructively used. The powers at Microsoft have made it clear that they won’t be putting the syntax of C# itself up for grabs - and for good reason.

None the less …

… there are some sparkling possibilities - and here are my thoughts on some of them.

When the async and await keywords were introduced in C# 5, one of the reasons the C# language team took that particular approach was to use an “opt-in” approach, so that existing code wouldn’t be adversely affected by the new capabilities.

Building on this idea, what if we were able to build in some kind of transactional support into the language thusly:

public transactional void CompleteSale()
{
	transaction
	{
		// Do stuff here
	}
	commit
	{
		// Save changes permanently
	}
	rollback 
	{
		// Undo the changes
	}
}

If we wanted to make this happen, leveraging the Rosylin toolset, perhaps it might look like this:

[LanguageKeyword("transactional")]
public class TransactionalMethodExtension : MethodExtension
{
	[LanguageExtension]
	public Block CreateTransactionalBlock(
		[LanguageKeyword("transaction")]
		Block transactionBlock,
		[LanguageKeyword("commit")]
		Block commitBlock,
		[LanguageKeyword("rollback")]
		Block rollbackBlock
	{
		// Funky stuff goes here
	}
}

Points of interest …

  • The [LanguageKeyword] attribute used to specify the keyword required on the method declaration to bring in this particular method extension
  • MethodExtension would be some kind of required base class that provides supporting infrastructure.
  • The [LanguageExtension] attribute identifies a method that provides an extension. In this case, our method takes three “blocks” and combines them together into a single one.
  • Simply having this class in project - or in a referenced assembly - would be enough to make it available for use, but because it’s opt-in the only methods that are affected would be those that want it.

Of course, the classic - and perhaps the simplest - application would be for Notifying properties:

[LanguageKeyword("notifying")]
public class NotifyingPropertyExtension : PropertyExtension
{
	// Modify the passed Setter into a form that throws the event
	public override Block ExtendSetter(Block setterBlock)
	{
		// More funky stuff goes here
	}
}

This would allow a very simple declaration:

public class Person
{
	public notifying string FullName { get; set; }
	public notifying string FamilyName { get; set; }
	public notifying string KnownAs { get; set; }
}

Comments

blog comments powered by Disqus
Next Post
HTML to Markdown with Powershell  24 May 2014
Prior Post
Simpler code with DirectoryInfo  13 May 2014
Related Posts
Browsers and WSL  31 Mar 2024
Factory methods and functions  05 Mar 2023
Using Constructors  27 Feb 2023
An Inconvenient API  18 Feb 2023
Method Archetypes  11 Sep 2022
A bash puzzle, solved  02 Jul 2022
A bash puzzle  25 Jun 2022
Improve your troubleshooting by aggregating errors  11 Jun 2022
Improve your troubleshooting by wrapping errors  28 May 2022
Keep your promises  14 May 2022
Archives
May 2014
2014