[Scilab-Dev] problem with function overloading
Samuel Gougeon
Samuel.Gougeon at univ-lemans.fr
Wed Mar 10 15:03:30 CET 2010
Hi Stéphane,
----- Message d'origine -----
De : Stéphane Mottelet
Date : 02/03/2010 09:35:
> Hi all,
>
> I have some difficulties to understand how overloading of functions
> (not operators) works.
> E.g. I would like to redefine delete() for handles, which is done
> actually with %h_delete.
> (BTW, when I look at the source i see some calls to delete with handle
> arguments, how does
> scilab cope with apparent recursivity here ?). When I try to redifine
> %h_delete in a
> library loaded at startup time, then the original %h_delete is still
> used instead of the
> "new" one. How can solve this problem (I also need to redefine %h_set
> and %h_get...)
>
> thanks for help
>
> S.
AFAIU, overloading enables (and is aimed) to _extend_ a function in
order to
handle _new data types_, not to redefine it to the already supported
data types.
By default, whether arguments are of the "default" supported data types,
the
regular definition of the primitive is used and applied. Otherwise, and
only then,
the overloading definition is applied.
There is no reason that SCI/modules/gui/%h_delete exists, since the "h"
= graphic
handle data type is the default one for the delete() primitive. So,
%h_delete likely
cannot be called by scilab, unless maybe it is aimed to be used with a
_vector_ of
handles instead of a scalar handle (whether the primitive is defined
only for a scalar
handle as input argument).
For redefining a primitive, you should overwrite it with a macro.
Now, if you want to use the initial definition of the primitive inside
its own redefinition,
you should first define an alias of the primitive. Here is an example
for the max()
function. Its default definition returns the position only of the first
occurence of the
max value, while i need the position of all occurences. Therefore, i
want to redefine
max() as well as to use the original max() primitive inside it. I will do :
m=grand(10,5,"uin",0,9) // Create data for test
[v,ij]=max(m) // test before redefinition of max()
Max=max; // Creates an alias.
deff("[v,k]=max(m)","v=Max(m); k=ind2sub(size(m), find(m==v));") //
redefines max() using the alias
[v,ij]=max(m) // Applying the new max()
[v,ij]=Max(m) // Applying the former max()
// max=Max; // Restoring the old max()
Note that it is not possible to create the alias inside the
redefinition, since
the definition is overwritten prior to be called ;)
Then, the Max=max; may be done for instance in the scilab.ini (i did not
try).
HTH
Samuel
PS: Because overloading is not aimed to redefine, i am not sure that a
bug report is relevant.
More information about the dev
mailing list