[Scilab-users] Calling Function from Structure Field

classic Classic list List threaded Threaded
4 messages Options
Tan Chin Luh Tan Chin Luh
Reply | Threaded
Open this post in threaded view
|

[Scilab-users] Calling Function from Structure Field

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
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
mottelet mottelet
Reply | Threaded
Open this post in threaded view
|

Re: Calling Function from Structure Field

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
[hidden email]
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

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Samuel GOUGEON Samuel GOUGEON
Reply | Threaded
Open this post in threaded view
|

Re: Calling Function from Structure Field

In reply to this post by Tan Chin Luh
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
[hidden email]
http://lists.scilab.org/mailman/listinfo/users



_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Tan Chin Luh Tan Chin Luh
Reply | Threaded
Open this post in threaded view
|

Re: Calling Function from Structure Field

Thanks Stephane and Samuel for the prompt reply, so I could move forward with the functional programming method instead of keep trying to emulate the OOP way. 

Best Regards,
Chin Luh


_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users