Created
May 31, 2013 05:29
-
-
Save akimboyko/5683081 to your computer and use it in GitHub Desktop.
Nemerle macro sample: new syntax keyword `fault`
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} | |
]> | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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