[nem-en] Late Binding in Nemerle
Michal Moskal
michal.moskal at gmail.com
Sat Jul 1 19:34:40 CEST 2006
On 7/1/06, Snaury <snaury at gmail.com> wrote:
> Hi everyone,
>
> I was trying to grok nemerle's macros for some time when an idea
> suddenly popped in my mind how I could do late binding aka dynamic
> typing aka duck typing in a pretty simple and (maybe?) rich way. So I
> wrote a macro for this, source code here:
> http://www.furry.ru/kitsu/nemerle/Kitsu.LateBindingMacro.n.txt
>
> It allows to use late binding on (I think) any expression, you just
> have to wrap it with late, and use like this:
>
> late(expr).CallMe(param1, param2, param3).CallOtherMe(param, param,
> param).Property[param, param, param] = value
This seems very nice! And indeed a showcase for Nemerle macros.
> It also supports ref/out and named parameters:
>
> late(expr).CallMe(namedparameter = ref variable, out variable, and
> the like...)
>
> though one needs to be very careful with named parameters, because
> when you name a parameter, other (unnamed) parameters might get
> shifted, for example in function:
>
> public void SomeFunc(a : int, b : int) : void
>
> when you use
>
> late(obj).SomeFunc(v1, a = v2)
>
> SomeFunc will be called as SomeFunc(v2, v1), i.e. it won't produce any
> errors, it would just silently accept it.
I don't quite get how you handle named parameters.
The perfect solution would be:
class A
public foo (a : int, b : int) : void { }
class B
public foo (b : int, a : int) : void { }
def o : object = if (some_cond) A() else B()
late(o).foo(a=3,b=7)
here we should call A.foo(3,7) or B.foo(7,3). So the choice happens at
run time. However I doubt this: a/ is easy to do, b/ can be efficient,
c/ is really useful. So it should be OK to just disallow it.
> Also, there's not much error checking there (and to be honest I don't
> even know how I can do more error checking from within a macro),
You can invoke the typer from inside the macro, but not from the code
that macro generates. So I think we need to live with the runtime CLR
checks.
> and
> if something goes wrong, you'll probably won't even know where exactly
> error happend, none the less I was extremely amazed at WHAT I could do
> with Nemerle's macro syntax so I though I'd share it with other people
> like me, who might really need to use late binding in their programs.
> =^_^=
>
> Now I can say it even more: Nemerle Rocks!
>
> P.S.
> If someone can help me to improve it, help in form of patches would be
> greatly appreciated :)
I think it would be great if you can add a wiki page about it (under
Macro packages).
>
> P.P.S.
>
> When I was searching the net on any info about duck typing in Nemerle
> I occasionaly found this particular statement:
>
> http://nemerle.org/irc/nemerle-2006-01-09
> 13:50 <malekith> the main differences between boo and nemerle is that
> we do not allow dynamic typing to sneak at without notice, we have
> better support for metaprogramming (quotations) and compiler written
> in Nemerle (booc is in C#)
>
> After reading it I'm even a little scared if my idea of dynamic typing
> would be met with knifes and banished like something evil. All I'm
> trying to do is to give power when one needs it, especially when one
> *really* needs it. x_x
With your solution the dynamic typing cannot just sneak in -- you need
to explicitly use duck(...) :-) BTW, I think we should stick to either
duck() or late().
BTW, instead of:
| x :: xs when x is LateNode.Assign => ... x :> LateNode.Assign
you can use
| x is LateNode.Assign :: xs => ... x
You can also use variant pattern on assign (but you probably know about it).
--
Michał
More information about the devel-en
mailing list