Hello Chin Luh,

Yes, it's a very good question. As far as i know -- and i tried various things, as you did --, this is currently not possible. The missing feature is the "this" object, refering to the /parent/ structure of which the function is a field. This would be a very nice feature, enabling some OO-like coding features like this one
(without the full OO philosophy with classes, etc).
This would also be an equivalent of the .parent property of graphics handles. So maybe, when creating a structure or another container with fields, this field could be implicitly implemented?

I thought that this feature was reported as a wish somewhere either on users@ or in Bugzilla, but i am not able to find it back.
Regards

Samuel

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@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users


_______________________________________________
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users

Reply via email to