[Scilab-users] Calling Function from Structure Field

Stéphane Mottelet stephane.mottelet at utc.fr
Wed Jul 1 10:57:01 CEST 2020


Hi,

--> my_struct.func(my_struct)

is the only way. This is not possible to it the way you would like, it 
would need real object-oriented features which Scilab does not have.

At least, we could mimic this if field extraction could be overloaded.

S.

Le 01/07/2020 à 05:44, Chin Luh Tan a écrit :
> Hi,
>
> I am wondering if this is possible:
>
> I've a structure with data and function pointer, for example:
>
> mystruct.data = 5;
> mystruct.func =%myfun
>
> calling mystruct.func() will call myfun with the input of mystruct.data.
>
> To put it in example, this is what I tried:
>
> //  define an inline function
> --> deff('x = myfunc(y)', 'x = y.data + 1')
>
> // create my_struct with field 'data' = 2
> --> my_struct.data = 2
> my_struct  =
>
>   data = 2
>
> // try to call myfunc with the my_struct -->o k
> -->  myfunc(my_struct)
> ans  =
>
>    3.
>
> // Define a "pointer" to myfunc
> --> %ptr = myfunc
> %ptr  =
>
> [x]=%ptr(y)
>
> // Try to use %ptr to call the function --> ok
> --> %ptr(my_struct)
> ans  =
>
>    3.
>
> // assign %ptr to my_struct.func
> --> my_struct.func = %ptr
> my_struct  =
>
>   data = 2
>   func: myfunc(y) => [x] (3 lines)
>
>
> // I still can call the function with this, but it looks redundant as 
> "my_struct" appear twice..
> --> my_struct.func(my_struct)
> ans  =
>
>    3.
>
> // How to I modified the codes so that calling below line will 
> automatically find the my_struct.data?
> --> my_struct.func()
> at line     2 of function myfunc
>
> Undefined variable: y
>
>
> Thanks for advice.
>
> Regards,
> Chin Luh
>
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> https://antispam.utc.fr/proxy/1/c3RlcGhhbmUubW90dGVsZXRAdXRjLmZy/lists.scilab.org/mailman/listinfo/users

-- 
Stéphane Mottelet
Ingénieur de recherche
EA 4297 Transformations Intégrées de la Matière Renouvelable
Département Génie des Procédés Industriels
Sorbonne Universités - Université de Technologie de Compiègne
CS 60319, 60203 Compiègne cedex
Tel : +33(0)344234688
http://www.utc.fr/~mottelet

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20200701/49a5e202/attachment.htm>


More information about the users mailing list