How to overload operator()?

9 views
Skip to first unread message

jneeman

unread,
Aug 2, 2010, 11:02:30 PM8/2/10
to Nemerle Forum
Given a class Foo, how can I define the equivalent (in C++) of
Foo::operator()? I've tried
public static @() (a: Foo, b: int) {}
and
public static @(a: Foo, b: int) {}
but I get syntax errors from both.

Thanks,
Joe

catbert

unread,
Aug 3, 2010, 4:19:56 AM8/3/10
to Nemerle Forum
I don't really think you can do it currently.

I tried the implicit casting trick:
class MyFun
{
public Invoke(_ : string) : int
{
42
}

public static @:(fn : MyFun) : string -> int
{
| null => null
| _ => fn.Invoke
}
}
But it failed: "the value called (mf) has non-functional type
Program.MyFun".

Probably we could resolve this by changing type inference engine a
little. But at the time you are just stuck with using indexers (a[x])
or looking for another solution.

winnie

unread,
Aug 3, 2010, 1:28:28 PM8/3/10
to Nemerle Forum

On 3 Сер, 06:02, jneeman <joenee...@gmail.com> wrote:
Unfortunately on .NET and even in Nemerle's extension you can't
overload operator (). You should use mabaged C++ (in .NET) but it
will be visible only to that project.


Perhaps you may be interested in indexer, it's almost same except that
you should use square braces!

class Foo
{
public Item[b:int]:int
{
get
{ b*5 }
}
}


and then:

def f = Foo();

def i = f[5];

Joe Neeman

unread,
Aug 3, 2010, 3:40:27 PM8/3/10
to nemer...@googlegroups.com
Thanks for the responses. I guess indexers will do for now.

Cheers,
Joe

> --
> nemerle-en Google Group, try http://groups.google.com/group/nemerle-en
> for more options.


Reply all
Reply to author
Forward
0 new messages