[Scilab-users] Emulate Object Oriented Programming

Mike Page Mike at Page-One.Waitrose.com
Tue Nov 27 11:10:30 CET 2012


You could of course "define constants" in Scilab.  This is what I usually do:

	[NONE, CONTINUOUS, DISCRETE, FFT] = (1, 2, 3, 4);

OK - not perfect, but it makes code more readable.

HTH,
Mike.


-----Original Message-----
From: users-bounces at lists.scilab.org
[mailto:users-bounces at lists.scilab.org]On Behalf Of
michael.baudin at contrib.scilab.org
Sent: 27 November 2012 09:08
To: users at lists.scilab.org
Subject: Re: [Scilab-users] Emulate Object Oriented Programming


Hi,

Thank you Serge for this interesting script.
Defining a tlist with unnamed fields is indeed an idea which
is quite unusual !

You will find in attachment a modified version, merging your proposal 
and my
previous attempt.

On the user's side, the "object" can now produce messages such as:

-->bob.weight=-5
  !--error 10000
Expect nonnegative weight.
at line      18 of function %s_i_THUMAN called by :
bob.weight=-5

which is excellent.

On the developper's side, the "set" function (which is
called insertion in the overloading vocabulary), is now
much less clear, because fields are now identified with
their index (i.e. 1, 2, 3,...) instead of their name
(i.e. "name", "weight", ...).

For example, this is the code to set the "weight" field:

function h=%s_i_THUMAN(field,value,h)
[...]
     select field
     case "weight"
[...]
         if (value<0) then
             error("Expect nonnegative weight.")
         end
         h(4)=value
[...]

So this is not an ideal solution: it has some advantages for the
users, but is much more difficult for the developper.
It is a huge source of bugs, for example if we mix up the
indices: e.g. 2 instead of 4 will set the "name" instead of the
"weight".
In the C language, we would #define constants to these indices,
so that we can avoid this problem.
But this is Scilab, not C.

Isn't there a simpler way ?
In other words, couldn't we get the named fields both from the
user's and the developper's sides ?

Best regards,

Michaël

Le 2012-11-26 19:09, Serge Steer a écrit :
> For safe mode you can use tlist with no field names. See the example
> in the attached file
>
> Serge Steer
>
> -------------------------
>
>> DE: "Daniel Penalva" <dkajah at gmail.com>
>> À: "International users mailing list for Scilab." 
>> <users at lists.scilab.org>
>> ENVOYÉ: Dimanche 25 Novembre 2012 23:04:56
>> OBJET: Re: [Scilab-users] Emulate Object Oriented Programming
>>
>> Hi,
>>
>> By safe you mean that the field is protected or private ? i dont 
>> know if it is possible in scilab nowadays ...
>>
>> [ ]s
>>
>> On Sun, Nov 25, 2012 at 7:28 PM, <michael.baudin at contrib.scilab.org> 
>> wrote:
>>
>>> Hi,
>>>
>>> I have a problem with the possibility of emulating OOP in Scilab 
>>> with
>>> tlists, that prevents me to have safe "set" methods.
>>> In the script in attachment, I created a human "class" with
>>> two fields: name (a scalar string) and weight (a scalar real).
>>>
>>> The problem is : how have a "set" method which is both simple and 
>>> safe ?
>>>
>>> Here is how this class works :
>>>
>>> bob=human_new()
>>>
>>> This is simple, but is unsafe:
>>>
>>> bob.name [1]="Bob"
>>> bob.weight=70
>>> bob.name [1]=-12 // Oups !
>>>
>>> These statements are safe:
>>>
>>> bob=human_set(bob,"name","Will")
>>> bob=human_set(bob,"weight",80)
>>>
>>> It is safe in the sense that the following statements produce an 
>>> error:
>>>
>>> bob=human_set(bob,"weight",-12)
>>>
>>> The function "human_set" is safe but somewhat unconvenient to use.
>>>
>>> The question is :
>>>
>>> How to make so that bob.name [1]="Will" makes the code 
>>> bob=human_set(bob,"name","Will") be executed ? Can overloading do 
>>> this ?
>>>
>>> Is the only possible way is at the C level with the sci_percent* 
>>> functions that Denizet wrote :
>>>
>>> 
>>> http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/xml/sci_gateway/cpp/sci_percent_XMLAttr_size.cpp;h=9d3b361bcbe6416e62f422dd448aa72d65f1fe4c;hb=HEAD 
>>> [2]
>>>
>>> for the XML module ?
>>>
>>> Best regards,
>>>
>>> Michaël
>>>
>>> _______________________________________________
>>> users mailing list
>>> users at lists.scilab.org
>>> http://lists.scilab.org/mailman/listinfo/users [3]
>>
>> --
>> Democracia Digital Direta
>> Carta:
>>
>> http://li7e.org/ddd2 [4]
>>
>> Des Carta coletiva aos ministerios
>> 
>> http://rede.metareciclagem.org/blog/16-10-12/Des-Carta-da-Rede-Metareciclagem-para-o-Ministerio-da-Cultura-e-Outros-Ministerios-Tam 
>> [5]
>>
>> AFROAMBIENTAL EH SOCIEDADE EM AXE E DIVERSIDADE
>>
>> http://afroambiental.org [6]
>>
>> Daniel Penalva
>>
>> State related activity, currently:
>> Phd - Physics in Institute for Theoretical Physics - 
>> http://www.ift.unesp.br/posgrad/ramais-alunos-pos.php [7]
>>
>> Transparency portal(workflows): 
>> http://www.nightsc.com.br/aa/interface_v0.1.php [8] *look for SUoU9 
>> user, or do ctrl+f and SUoU9*
>>
>> FLOSS and related ideas enthusiastic
>>
>> _______________________________________________
>> users mailing list
>> users at lists.scilab.org
>> http://lists.scilab.org/mailman/listinfo/users
>
>
>
> Links:
> ------
> [1] http://bob.name
> [2]
> 
> http://gitweb.scilab.org/?p=scilab.git;a=blob;f=scilab/modules/xml/sci_gateway/cpp/sci_percent_XMLAttr_size.cpp;h=9d3b361bcbe6416e62f422dd448aa72d65f1fe4c;hb=HEAD
> [3] http://lists.scilab.org/mailman/listinfo/users
> [4] http://li7e.org/ddd2
> [5]
> 
> http://rede.metareciclagem.org/blog/16-10-12/Des-Carta-da-Rede-Metareciclagem-para-o-Ministerio-da-Cultura-e-Outros-Ministerios-Tam
> [6] http://afroambiental.org
> [7] http://www.ift.unesp.br/posgrad/ramais-alunos-pos.php
> [8] http://www.nightsc.com.br/aa/interface_v0.1.php
>
> _______________________________________________
> users mailing list
> users at lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users




More information about the users mailing list