[nem-en] Late Binding in Nemerle

Snaury snaury at gmail.com
Sat Jul 1 21:42:55 CEST 2006


On 7/1/06, Michal Moskal <michal.moskal at gmail.com> wrote:
> I don't quite get how you handle named parameters.

Actually I don't really handle them. Type.InvokeMember gets array of
parameters and array of parameter names (they don't have to be equal
size, if they are not, then only [length of names array] first
parameters are named and the rest are positional). I just move all
named parameters to the head of the list leaving positional parameters
after them, and pass array to InvokeMember. Default binder does the
rest for me. For example:

  .foo(1, b = 2, 3, d = 4, 5, f = 6)

First turns into:

  .foo(b = 2, d = 4, f = 6, 1, 3, 5)

And then processed and passed to InvokeMember like this:

  .InvokeMember(... [2, 4, 6, 1, 3, 5], ...., ["b", "d", "f"], ...)

> 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.

Well, my solution is purely late bound (I specifically didn't want to
analyze anything and hand everything over to default binder, because
all of such checking will have to be done at runtime, polluting
generated IL with a lot of [in most cases] unnecessary code), so yes,
it will be either A.foo(3, 7) or B.foo(7, 3), bepending on instance -
.NET's binder decides.

> I think it would be great if you can add a wiki page about it (under
> Macro packages).

Well, some time later maybe. :)

> BTW, instead of:
>
>   | x :: xs when x is LateNode.Assign => ... x :> LateNode.Assign
>
> you can use
>
>   | x is LateNode.Assign :: xs => ... x

Ah! That's what I thought too, but it gave me error that
LateNode.Assign :: xs is not a legal expression. Now I tried | (x is
LateNode.Assign) :: xs and surprisingly it works (I thought I tried
that, but maybe not), thanks! =)



More information about the devel-en mailing list