Skip to content

Instantly share code, notes, and snippets.

@akimboyko
Created May 31, 2013 05:29
Show Gist options
  • Save akimboyko/5683081 to your computer and use it in GitHub Desktop.
Save akimboyko/5683081 to your computer and use it in GitHub Desktop.
Nemerle macro sample: new syntax keyword `fault`
using Nemerle;
using Nemerle.Collections;
using Nemerle.Compiler;
using Nemerle.Text;
using Nemerle.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
namespace MetaProgramming.Nemerle.Macro
{
macro execute(body, actionOnFault, actionOnFinally)
syntax (
"try",
body,
"fault",
actionOnFault,
"finally",
actionOnFinally)
{
<[
try
{
$body;
}
catch
{
| e is System.Exception => { $actionOnFault; throw; }
}
finally
{
$actionOnFinally;
}
]>
}
}
using Nemerle;
using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using MetaProgramming.Nemerle.Macro;
namespace MetaProgramming.Nemerle
{
public partial class FaultKeywordSample
{
public ExecuteFaultCSharpNemerle(body : Func[string], onFault : Action, onFinally : Action) : string
{
mutable result : string = null;
try
{
result = body();
}
fault // here is a _new keyword_ `fault` only executed after exceptions
{
onFault();
}
finally
{
onFinally();
}
result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment