[Gambas-user] Suggestions 4 new keywords

Doriano Blengino doriano.blengino at ...1909...
Wed Sep 15 14:51:25 CEST 2010


Fabián Flores Vadell ha scritto:
> Hi Benoît. Below I expose some suggestions for new and alternative keywords.
>
> PUBLIC and PRIVATE keywords defines the scope for attributes and
> methods, but this words come from (or evokes) modular/structured
> programming. Because PUBLIC methods and attributes (and properties)
> define the interface for a class, I think that a good and alternative
> syntax could be based in new keyword like INTERFACE, IMPLEMENTATION,
> METHOD, ATTRIBUTE, PROPERTY, VALIDATE, etc.
>
> Example:
>
> PUBLIC Name AS String
> PUBLIC LastName AS String
>
> PRIVATE iAge AS Byte
>
> PROPERTY Age AS Byte
>
> PRIVATE FUNCTION Age_Read() AS Byte
>    RETURN iAge
> END
>
> PRIVATE SUB Age_Write(Value AS Byte)
>    IF Value>= MinAge THEN
>      iAge = Value
>    ELSE
>       Error.Raise("Age have to equal or greater than: "&  MinAge)
>    ENDIF
> END
>
> PUBLIC SUB DoSomething()
>    . . .
> END
>
> PUBLIC FUNCTION DoSomethingElse() AS String
>    . . .
> END
>
> PRIVATE SUB DoSomething1()
>    . . .
> END
>
> PRIVATE FUNCTION DoSomethingElse1() AS String
>    . . .
> END
>
> -----------------
>
> Become:
>
>
> INTERFACE
>
>    'Attribute keyword is optional
>    ATTRIBUTE Name, LastName AS String
>
>    'Defines an attribute that is part of interface class and is
> accessed by dot notation (AnObject.Age, AnObject.Age=34)
>    PROPERTY Age AS Byte
>      'Validate when property is written (WHEN WRITE is optional because
> is the default)
>      'When Write: just in case, so When Read could exist too.
>      VALIDATE WHEN WRITE Age>= MinAge
>        'Unsucessfull is a long word, may be there's other better
>        UNSUCESSFULL Error.Raise("Age have to equal or greater than: "&  MinAge)
>
>    'METHOD keyword is optional
>    METHOD DoSomething()
>      . . .
>    END
>
>    METHOD DoSomethingElse() AS String
>      . . .
>    END
>
>
> IMPLEMENTATION
>
>    CONST MinAge AS Byte = 28
>
>    . . .
>
>    'Same name is used in INTERFACE: this would possible because the
> scope is different
>    METHOD DoSomething()
>      . . .
>    END
>
>    METHOD DoSomethingElse() AS String
>      . . .
>    END
>
> ------------
> Advantages
>
> * More expressive code (smells a bit declarative)
> * Is needed to write less code (50% in this example)
> * keywords are closer to OOP
> * More easy to teach
>
> Disadvantages
>
> * This code not seems BASIC neither Gambas (that is why this syntax
> should be alternative)
> * Is difficult to implement?
>
>
> What do you think?
>    
Probably with "you" you mean Benoit and not me, anyway...

Your proposal resembles the syntax of pascal - I like pascal, and use it 
a lot; but the biggest annoyance of pascal is this double declaration in 
interface and implementation: very clean and very logical - for a 
compiler from 1970. I don't think that substituting PRIVATE and PUBLIC 
with INTERFACE and IMPLEMENTATION would save typing, may be sometimes...

The validation mechanism seems pretty, but does not add anything to the 
more general form of getter/setter. Just more readable by a human (which 
is not too little, anyway).

May be that there can be a midway idea: to have PUBLIC and PRIVATE 
inherited from the last declaration. You specify PUBLIC at the 
beginning, and then write variables and methods. Then you specify 
PRIVATE, and then write all the private part of the class. This way, you 
get closer to your proposal, still having the possibility to mix public 
and private declarations.

To Fabien:
> All variable are in top of class and local in top of procedure ...
>
> then we have in gb3 an explorer that show the class content ...
>
>    
Having to declare *all* the variables together, before they are used, is 
sometimes limiting, expecially when speaking about class variables. Why 
not group together variables and related routines? This is in fact 
doable in gambas, and much less in pascal. You could argue that a class 
should do only one thing, so there would be no need to create different 
groups - but I don't think so... especially with the model of gambas 
which does not really "import" a module or a class.

Regards,
Doriano






More information about the User mailing list