[Gambas-user] Suggestions 4 new keywords
Benoît Minisini
gambas at ...1...
Thu Sep 16 16:52:24 CEST 2010
> 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?
>
(1) First, you must realize that the syntax is not the implementation. In a
more general way, the word is not the thing. We are confused by that all the
life. :-)
So, replacing one word (for example PUBLIC) by another (ATTRIBUTE), whereas
the implementation is the same behind is not a good idea, because then this is
not BASIC anymore.
I don't mean that the current syntax better, but that as I chose to implement
a BASIC, I have to follow something that looks like the common usages in other
languages.
If I had decided to create a new syntax from scratch (usually people says 'a
new language from scratch'), of course things would be different. I would have
used the C syntax like Java or Javascript. :-)
(2) I find your VALIDATE / UNSUCCESSFUL... useless, as you introduce a lot of
syntactic sugar for something that is actually not so frequent.
(3) On the other hand, it might be a good idea to allow the user to separate
the public and the private thing.
Note that I am not agree with the common separation between "interface" and
"implementation". In the real life, they are not separated. A good interface
usually leads directly to the implementation. Or the interface surface from a
good implementation. :-)
Doriano's idea may be good: when PUBLIC or PRIVATE is alone on his line,
everything after is declared PUBLIC (or PRIVATE) by default. That way, you can
write:
PUBLIC
' The "interface"
' ...
PRIVATE
' The "implementation"
' ...
Maybe the right way to deal with code organization is an editor that is not
line-based, but that split the source code between its different parts, and
present it to the user in a hierarchical way.
But as long as I am faster with a normal text editor, I won't go that way. :-)
Regards,
--
Benoît Minisini
More information about the User
mailing list