Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: [ A world of words ] naming convention, namespace, namespace pollut

From: rebol:meurrens at: 4-Jun-2003 0:54

Hi Gregg, Thank you for your interest in this matter. I was surprised (and very happy) by your result. Unfortunately there is a small mistyping (the ":")
>> o3/pri=: 33 >== 33
You did not use the "set method" ; you just replaced it by 33 .... :-( Anyway, here is the example completely rewritten for all the readers who did not follow this thread since the beginning. This makes clear, for those not aware of our OO vocabulary, what we mean by "static" and by "instance". A more pedagogical example (the "tank") still come later in this message together with "what I want". REBOL [ ] use [ y z ] [ z: 3 ; PRIVATE STATIC WORD (thanks 'use) obj: make object! [ =z: does [ z ] z=: func [ a ] [ z: a ] =x: x=: none use [ x ] [ x: 1 ; PRIVATE STATIC WORD (not instance) =x: does [ x ] x=: func [ a ] [ x: a ] ] y: 2 ; PUBLIC INSTANCE WORD (despite 'use) =y: does [ y ] y=: func [ a ] [ y: a ] w: 4 ; PUBLIC INSTANCE WORD =w: does [ w ] w=: func [ a ] [ w: a ] ] ] other: make obj [ ]; another instance ; x is PRIVATE : obj/x ===> error ; x is STATIC ... obj/x= 11 other/=x ; ===> 11 ; y is PUBLIC : obj/y ; ===> 2 ; y is INSTANCE ... obj/y= 22 other/=y ; ===> 2 (unchanged) ; z is PRIVATE : obj/z ===> error ; z is STATIC ... obj/z= 33 other/=z ; ===> 33 I would hazard a guess that you can acheive what you want (once we
>know exactly what that is).
This would be nice. Let us discuss of the "tank" example. It's a very simple example I often use to illustrate OO, and how OO can be implemented in some language. If you, or another reboller, has a solution to this "school case" this would be marvellous.... ;;;;;;;;;;;;; WHAT I WANT... THE "TANK" EXAMPLE use [ _capacity ] [ _capacity: 100 ; max number of gallons in any tank ;;; "STATIC" tank1: make object! [ _content: 0 ; current nr of gallons in this very tank ;;; each "INSTANCE" has a different content content+: func [ add ] [ _content: max 0 min _capacity _content + max 0 add ] content-: func [ sub ] [ _content: max 0 min _capacity _content - max 0 sub ] =content: does [ _content ] =capacity: does [ _capacity ] ] ] tank2: make tank1 [ ] ; two different instances tank1/content+ 50 ; add 50 gallons in tank 1 tank2/content+ 80 ; add 80 in tank 2 tank2/content- 20 ; substract 20 from tank 2 tank1/content+ 999 ; try (!!!!!) to add 999 in tank 1 (much will go on the floor...) tank1/content- 5 ; substract 5 from tank 1 tank1/=content ; ===> 95 (try with your own car: 0 + 50 + 999 - 5 ===> 95 !!!) tank2/=content ; ===> 60 ( 0 + 80 - 20 ) ;;;;;;;;;;;;;;;; I WANT TO AVOID THAT WE MAY WRITE THIS HORRIBLE CODE: cash: -500 ; i now allow negative values for cash due to my new credit policy ; .... and 425 lines below... price: 30 ; price of gas in $ per gallon ; .... and 212 lines below... a: cash / price ; use all our money to buy gas. how much gallons may we receive? ; ... and 723 lines below... tank1/_content: a ; now the tank holds a negative number of gallons... ; later on, we'll use tank1/_content or tank1/=content ; to compute a negative distance... ; etc, etc... and somewhere else we'll crash our car on a wall... !!! ;;;;;;;;;; BY AVOIDING A DIRECT ACCESS TO _content == BY ENCAPSULATING _content (for those who are new to OO concepts/vocabulary, the '_content is encapsulated with appropriate methods; mind that there is no "set" method and that the increase and "decrease" methods carefully check argument and result)
>MM> My own convention is to prefix their name with an "underscore" >MM> and to destroy with energy any script that would contain >MM> a "/" followed by a "_" >MM> Something very easy to detect automatically :-) > >I've done the same thing myself in a number of places. Of course, then >I found one place where I wanted to actually use a leading underscore >with words in an object, so I'm still considering what the best >approach or convention is for this kind of thing.
;;;;; THE ONLY WAY I FOUND FOR INSTANCE VARIABLES IN REBOL ;;;;; IS THE CONVENTION "private begins with _" ;;;;; (that I also use in other languages) ;;;;; and then to detect the presence of "/" followed by "_" in scripts. ;;;;; WHY ? NOT AGAINST MALICIOUS SCRIPTS, ;;;;; BUT JUST TO PRODUCE ROBUST/READABLE/REUSABLE CODE ; a "malicious" script may decide anyway that it will not respect the convention ; a "malicious" script may anyway use 'set and access to everything, ; even things "protected" by 'use ; and even without using "/" followed by "_" ;; example: give a negative value to the _capacity :
>> probe tank1 >> make tank1 [ set first second :=capacity -999 ] >> tank2/=capacity
== -999 See also Volker's "malicious" example in message 29138 of this thread. Regards, </marc>
>-- Gregg >
Prof. Ir Marc Meurrens, Brussels (be) TEL: +32 (0)2 537 2812 FAX: +32 (0)2 537 7645 EMAIL: [marc--meurrens--org] URL: http://www.meurrens.org/ REB: http://rebol.mksa.net/ PGPKEY: http://www.meurrens.org/pgp/ Please don't mail me attached files, instead, use my 'exchange area' : EXCHANGE AREA: http://www.meurrens.org/exchange/ (HTTP/FTP upload/download of temporary/persistent files)