Forums

howto method reference

General discussion about Cobra. Releases and general news will also be posted here.
Feel free to ask questions or just say "Hello".

howto method reference

Postby oravard » Tue Apr 01, 2008 7:41 am

Hi,

I recently discovered Cobra and I found it very interesting although I am not very
familiar with .NET
I am trying to do some tests and I want to pass to a method a reference
to another method.
The objective is to do smething like that :

class MyClass
def method1(m)
...
if method1 <> nil
method1()
...
MyClass().method1(ref ob.other_method)

I know this latter example does not work since m is declared as dynamic
and there is a conversion problem.
I tried to use the MethodInfo class without sucess.

Can you help me for this general purpose ?

Thanks.
oravard
 
Posts: 7

Re: howto method reference

Postby Charles » Tue Apr 01, 2008 9:34 am

Yes. See the Sorter Example posted recently in this forum. Notice the use of "sig" rather than "def" to declare a "method signature". The name you use in that method signature becomes a type name. You'll then see that type name used subsequently in the declaration of an argument in order to accept a method reference.

Also, this is mentioned briefly in the Cobra 0.7.4 release notes.

Regarding your example, I would like to eventually make that work in Cobra, but for now passing methods around has to be strongly typed. Many other things can be dynamic however.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: howto method reference

Postby oravard » Tue Apr 01, 2008 1:44 pm

Thanks, it works.

There is another problem in my implementation. See the folowing code.
Is it a mistake from me or a compilator bug ? See the compilation error too.

Code: Select all
class MyClass
       sig BindMethod

       var _command as BindMethod

       def init(command as BindMethod)
             _command = ref command       #!! if I comment this line, there is no compilation error

class AnotherClass
       def method1
             print 'method 1'

       def main is shared
             p = AnotherClass()
             ob = MyClass(ref p.method1)


Here is the compilation error :
error: COBRA INTERNAL ERROR / UnknownMemberException / obj=Param(11344, "command" (ID), command, BindMethod), name='EnglishName', type=Param
Compilation failed - 1 error, 0 warnings

Thanks.
oravard
 
Posts: 7

Re: howto method reference

Postby Charles » Tue Apr 01, 2008 1:57 pm

oravard wrote:Is it a mistake from me or a compilator bug ? See the compilation error too.

Turns out to be both. Change "_command = ref command" to "_command = command". You need the "ref" to refer to dotted methods without invoking them. Once you have a value of the signature type, no "ref" is needed.

Think of it this way: "ref obj.foo" creates a pointer to the "foo" method. So you don't need "ref someVar" as someVar is already the pointer.

And there is a bug in the Cobra error message code which was trying to say "Only methods can be referenced, not variables." I'll fix.
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: howto method reference

Postby oravard » Tue Apr 01, 2008 2:10 pm

Fine !!

I have a few more question (that should be another thread) :

- can we define default values for method arguments ?
- can we use keyword arguments when calling methods ?
about this lattern, I saw a compiler message that says that will
be possible in the future.

Thanks.
oravard
 
Posts: 7

Re: howto method reference

Postby Charles » Tue Apr 01, 2008 4:24 pm

Currently, neither default values nor keyword arguments are implemented. I want to at least have keyword arguments for various benefits including making calls more readable, making the argument order flexible and passing key/value pairs.

But implementing these features requires a bit of extra thought because Cobra also has method overloading and it interfaces with C#. So everything has to fit together.

Note that overloading allows one to simulate simple default values:
class X

def foo(i as int) as int
return .foo(i, 1)

def foo(i as int, j as int) as int
return i * j

def main is shared
obj = X()
print obj.foo(2)
print obj.foo(2, 7)
Charles
 
Posts: 2515
Location: Los Angeles, CA

Re: howto method reference

Postby oravard » Tue Apr 01, 2008 4:28 pm

Yes. That what I used today. Thanks.
oravard
 
Posts: 7


Return to Discussion

Who is online

Users browsing this forum: No registered users and 21 guests