Yohanes Santoso <[EMAIL PROTECTED]> writes:

Saya mau bahas sedikit apa yang saya quote dari R5RS mengenai closure
karena ada beberapa hal yang mungkin membingungkan karena specific ke
Scheme.


> To each place where an identifier is bound in a program there
> corresponds a region of the program text within which the binding is
> visible.

Semua bounded identifier (variable name, class name, etc.) punya
lexical scope. Kata 'region' dipakai daripada scope karena sudah ada
pengertian dari bab Introduction bahwa Scheme adalah lexically-scoped
language. Dan region itu maksudnya a region of code as it appears on
the source code. Pemakai emacs tentunya tidak asing lagi dengan kata
region.

> The region is determined by the particular binding
> construct that establishes the binding; 

Scopenya ini tergantung sama bagaimana bounded identifier itu
di-bound. Misalnya identifiernya itu adalah variable name, scopenya
tentunya beda dengan identifier untuk class name. Bahkan juga jenis
variable juga mempengaruhi scopenya. Contohnya, di ruby local,
instance, class variables semuanya scopenya beda2x.

> if the binding is established by a lambda expression, for example,
> then its region is the entire lambda expression.

Ini Scheme specific. 

> Every mention of an identifier refers to the binding of the
> identifier that established the innermost of the regions containing
> the use. 

Identifier binding di-determine dari scope paling dekat
lexically. Jadinya misalnya ada 

module Foo
  class Foo
    p Foo
  end
end

Foo di line ke 3 refers ke class Foo, not module Foo.

> If there is no binding of the identifier whose region
> contains the use, then the use refers to the binding for the
> variable in the top level environment, if any (chapters 4 and 6);

Kalau tidak ada containing scope yang mengandung binding untuk identifier itu,
cari bindingnya di top-level environment. Ini analognya adalah global
scope di Ruby.

Containing scope adalah scope apa saja yang mengandung identifier
itu. Contoh:

module Foo1
  class Foo
  end
end


module Foo
  class Foo
    p Foo
  end
end


Untuk Foo di 'p Foo', containing scopesnya adalah: class Foo::Foo, module
Foo. class Foo1::Foo dan module Foo1 bukan containing scopesnya.

> if there is no binding for the identifier, it is said to be
> unbound.

Unbound bukan berarti error. Ada languages yang bisa inspeksi apakah
sebuah identifier bound atau tidak. Ada juga languages atau
implementations yang bisa lakukan custom handling kalau mendeteksi
penggunaan unbound identifier (berguna untuk debugging).


YS.

Kirim email ke