Compiler suggestion - Properties for modules

Coding and general discussion relating to the compiler

Moderators: David Barker, Jerry Messina

Post Reply
User avatar
octal
Registered User
Registered User
Posts: 586
Joined: Thu Jan 11, 2007 12:49 pm
Location: Paris IDF
Contact:

Compiler suggestion - Properties for modules

Post by octal » Wed Oct 17, 2007 8:34 am

Hi David,
I think that it woule be nice to think about adding properties to modules (just like delphi).

That means that in a module, now we MUST expose our variables as Public to let users modify them. Some times we add aliases for that.
The problem is that some times some registers/variables are linked in a way that to modify the value of one, we must modify one or two registers before it. In this case the only solution is to expose as PUBLIC a procedure Set...().

The elegant solutions should be to implement Properties with a setter procedure and a getter function.

How it should be (as I see it, mostly inspired by Delphi or C#) ? here is a simple example ...

The OLD (actual) Style

Code: Select all



Module myModule

Public dim ValSum as byte

private dim Val2 as byte  
private dim Val3 as byte

public sub SetValSum(pValue as byte) 
  ValSum = pValue
  Val2     = ValSum  * 2  // this proc sets also Val2 and Val3
  Val3     = ValSum - 1
end sub

public sub GetValSum()  as byte
  ValSum = Val2 + Val3 + ADC.Read()  // just an example
  result = ValSum
end sub


The NEW style using PROPERTIES
We can see that ValSum is useless since it has to be calculated (ADC.read) each time we need its value and Setting its value is usefull only to set Val2 and Val3.

The elegant way to do it using Properties

Code: Select all



Module myModule

Public PROPERTY ValSum as byte read GetValSum write SetValSum

private dim Val2 as byte  
private dim Val3 as byte


private sub SetValSum(pValue as byte) // no more need to be Public
  ValSum = Val2 + Val3 * pValue
end sub

private sub GetValSum()  as byte  // no more need to be Public
  ValSum = Val2 + Val3 + ADC.Read()  // just an example
  result = ValSum
end sub


Note the declaration

Code: Select all

Public PROPERTY ValSum as byte read GetValSum write SetValSum
This declaration means that ValSum DOES NOT Exists physically as a variable ... it means only that in calling code,
- when I use ValSum to GET its value (RValue in an expression) GetValSum is automatically called and replace the ValSum alias
- when I use ValSum to SET its value (LValue in an expression) SetValSum is automatically called and replace the ValSum alias, and the affected value is passed as a param to the SetValue procedure.

So using the module should be done using a call like that

Code: Select all

Program myProg
...

//old style 
dim b as byte

SetValSum(125)  // this sets also Val1 and Val2 in the module
b = GetValSum()  // 

// The new Styme
ValSum = 125  // this calls automatically SetValSum(125)
b = ValSum  // this calls automatically GetValSum()

Why using properties is better ?
because using them with Aliases let us write more generic modules, more modules that shares really the same interface, especially when setting Modules Internal variables ...
Lets say that you have written a module by exposing a variable MYVAR as PUBLIC. If it's used a lot in various programs, and at one time you discover that in some cases this variable should be constrained between 12 and 45 for example, instead of putting a message in the help file saying that this var should be normalized in a certain way, you should simply change it to a property and in the setter procedure make all your checks and normalize the value or trigger an error.

I think that this kind of feature gives more encapsulation to modules data and simplify their usage. This brings some advantages of OOP in Swordfish basic without having to deal with VMT or any OOP heavy feature. All calls and replacements should be fully resolved at compile time. The Getter and The Setter should be as simple as :
- The GETTER is a simple function implemented in the module and returning a result with the same type of the Property
- The SETTER is a simple SUB implemented in the module and accepting only one param with the same type of the Property

I hope it can be implemented (if yes ... PLEASE continue on the COF format ;) )

Regards
octal

Post Reply