[Scilab-users] Calling Function from Structure Field

Chin Luh Tan chinluh.tan at bytecode-asia.com
Wed Jul 1 05:44:48 CEST 2020


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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.scilab.org/pipermail/users/attachments/20200701/9310df3e/attachment.htm>


More information about the users mailing list