[nem-en] Macro proposal

Elifant elifantu at mail.ru
Tue Jun 19 10:23:58 CEST 2007


Hello all.

I just started to learn Nemerle. Thank you for this language, It is 
fantastic :)
I love indented syntax, but there are many functions which expect 
callbacks. So I need
to return to syntax with braces:

my_func1(arg, another, fun () {
   my_func2(arg, fun () {
     ...
   })
})

and so on. Is there proper way to write such code using indented syntax?

If not, here is small macro to simplify my coding:

#pragma indent

using Nemerle.Compiler
using Nemerle.Collections

macro @~~ (call, params ext_args: array[PExpr])
     match (call)
         | <[ $name (.. $args) ]> => <[ $name (.. $(args + 
List.FromArray(ext_args))) ]>
         | _ => Message.FatalError("There must be function call to the 
left of ~~ macro")

Usage:

def function_with_callback(a, b, func)
	func(a, b)

def sum = function_with_callback(1, 2) ~~ fun(l, r)
	l + r

It isn't restricted to callbacks:

def sum = function_with_callback(1) ~~ 2 ~~ fun(l, r)
	l + r

I was suprised that chained '~~' are converted to single macro call. But 
it is definitely convenient in this case.
Macro name isn't very good but I can't find any good one. :)
I propose this macro (or something similar) for adding to Nemerle 
library. Comments are welcome.




More information about the devel-en mailing list