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

[REBOL] [bind/synthesis/marc/16-May-2003] Patrick's pages ; using 'local ; using

From: rebol::meurrens::org at: 16-May-2003 9:39

Hello Romano, Gabriele, Patrick and the others, I agree with Romano: The concept of local words in functions as a different thing from arguments
>and refinements is only an appearance. >Strictly speaking, functions have arguments and refinements defined as local >words. They have not locals words which are not arguments or refinements.
In the 3 cases below, we may use as well 'm or 'local or 'a or 'b instead of 'n
>> a: 1 b: 2 c: 3
== 3
>> x: [ a b c ]
== [a b c]
>> do func [m n /local a b ][a: 10 b: 20 print bind x 'n ] 100 200 ; *** 1
10 20 3
>> do function [m n][a b][a: 10 b: 20 print bind x 'n ] 100 200 ; *** 2
10 20 3
>> do function [m n][a b][a: 10 b: 20 print bind x 'local ] 100 200 ; *** 3
10 20 3
>>
Thus the choice of the word used to indicate the context is quite arbitrary. If I use 'n , as in the examples 1 and 2, of course it works. But, if, later on, I modify my script I need to care about the word I effectively used to bind.
>> do func [nana m /local a b ][a: 10 b: 20 print bind x 'n ] 100 200 ;
*** 1 *** 'n removed from args but forgot in bind !!! ================== My suggestion (related to Patrick's pages) is to encourage the above form 3 a) - because the word 'local used in bind is a self documentation ! b) - because there is no need for an arbitrary choice between (in this example) 'm 'n 'local 'a 'b c) - because the word 'local is allways available in the circumstances where a bind is usefull either c-1) because you used the form 1 (func) and explicitely report the /local refinement or c-2) because this was done automatically for you in the forms 2 or 3 as already reported by Gabriele in this thread :
>> source function
function: func [ "Defines a user function with local words." spec [block!] {Optional help info followed by arg words (and optional type and string)} vars [block!] "List of words that are local to the function" body [block!] "The body block of the function" ][ make function! head insert insert tail copy spec /local vars body ]
>>
============ This thread discussed essentially the issue 1 of my initial message: http://www.escribe.com/internet/rebol/m31369.html in Patrick's page (see below), replace return do bind cb 'nb1 by return do bind cb 'local There was no message on the list on the issue 2: replace the line by: return do bind/copy cb 'local =============== My synthesis of this thread ======================== Thus, in the page http://www.pat665.free.fr/doc/exbind.html#sect3.1. After the definition of : fn-calculate: func [ nb1 [decimal!] nb2 [decimal!] nb3 [decimal!] cb [block!] ][ return do bind cb 'nb1 ] I should add the following (after reviewing my english typos...) ;;; in the above example 'nb1 was choised to define the local context ;;; but you could as well have used ;;; 'nb2 or 'nb3 or even 'cb it-self ! ;;; however, a more general definition of fn-calculate is proposed below: ;;; the 3 differences are: ;;; - we use function instead of func , and thus add an empty block between the signature and the body ;;; - we use 'local instead of 'nb1 ;;; - we use bind/copy instead of bind fn-calculate: function [ nb1 [decimal!] nb2 [decimal!] nb3 [decimal!] cb [block!] ][][ return do bind/copy cb 'local ] ;;; Mind that the word 'local used in the above example ;;; is a refinement produced automatically when using ;;; the 'function or the 'has words to build a function ;;; The double advantage of using 'local instead of 'nb1 ;;; is the readability (for everybody 'local means local !) ;;; and the fact that, if you modify your script, ;;; you don't need to modify the word used to bind. ;;; ;;; The use of bind/copy instead of bind is recommended ;;; to make sure you do not modify definitively ;;; the original version of ;;; your cb block: only a copy will be binded to your local ;;; function context. ================== In a similar approach to using 'local when binding within functions, the word 'self should be used within objects by preference over other arbitrary choised words. ============================================================== { We may have, in the future, another thread with a suggested head [bind 'self] if needed... } Thanks to all participants in this thread, Cordially </marc> 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)