[Gambas-devel] Proper way to the current object
Benoît Minisini
gambas at ...1...
Sat Feb 11 20:36:10 CET 2012
Le 11/02/2012 20:24, Benoît Minisini a écrit :
> Le 11/02/2012 20:23, Benoît Minisini a écrit :
>> Le 09/02/2012 15:56, Randall Morgan a écrit :
>>> Hi Benoit,
>>>
>>> I am a bit perplexed. I have some code that works and some that
>>> doesn't...
>>
>> If a method or a property returns a "GslComplex", then its signature
>> must use "GslComplex;" (or just "GslComplex" in a return value
>> signature). Do not use "o", this is an anonymous object that you should
>> use only if the method or property returns any object.
>>
>> And when you receive an object reference through GB_OBJECT, you must use
>> GB.CheckObject() to check it (to ensure it is not null).
>>
>> Regards,
>>
>
> Another point: Use NULL and not a void string for methods returning
> nothing, and/or having no arguments.
>
Other points again.
1) Why didn't you use gsl_complex directly in the GSLCOMPLEX structure?
2) You chose to create a new Complex each time you act on it.
Maybe this is not a good idea (for performance reasons). I think you
should better:
- Act on the current object without creating a new one, and return it
anyway.
- Add a Copy() method to copy a complex number when this is really needed.
So here is the "not-tested" result:
--------------------------------------------------------------------
typedef
struct {
GB_BASE ob;
gsl_complex c;
}
GSLCOMPLEX;
BEGIN_METHOD(GslComplex_Add, GB_OBJECT x)
GSLCOMPLEX *x = VARG(x);
if (GB.CheckObject(x))
return;
THIS->c = gsl_complex_add(THIS->c, x->c);
GB.ReturnObject(THIS); // GB.ReturnXXXX() methods return nothing!
END_METHOD
--------------------------------------------------------------------
The code is simpler, isn't it?
--
Benoît Minisini
More information about the Devel
mailing list