Date: Thu, 11 Aug 2005 08:12:58 -0300 From: romildo at uber.com.br To: Remi Vanicat Cc: lablgtk at math.nagoya-u.ac.jp Subject: Re: [newbie] Creating widget classes Message-ID: <20050811111257.GA7991 at malaquias> References: <20050811042345.GA29721 at malaquias> <6b8a914205081102267481b29b at mail.gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="ew6BAiZeqk4r7MaW" Content-Disposition: inline In-Reply-To: <6b8a914205081102267481b29b at mail.gmail.com> --ew6BAiZeqk4r7MaW Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Thu, Aug 11, 2005 at 11:26:27AM +0200, Remi Vanicat wrote: > 2005/8/11, romildo@uber.com.br : > > If I am not wrong, the button variable introduced > > by the let binding is common to all instances > > of the class (class variable). > > Well, the fact is that you are wrong. Those let binding let you define > variable that are bound before the intialization of the object, when > the instance variable are bound just before intialization method. > Otherwise, they (instance variable and let binding before the object > keyword) are private to each object. I am only partial wrong regarding the sharing of a variable binded in a let expression prior to the object keyword. As is shown by the attached test program. Its output is: $ ./t1 x1 has counter = 1 y1 has counter = 2 x1 has counter = 3 y1 has counter = 4 x2 has counter = 1 y2 has counter = 1 x2 has counter = 2 y2 has counter = 2 If the class has no parameter, class myclass = let myvar = ... in object ... end then the let is evaluated only once, when the class declaration is evaluated. In this case the class constructor is constant, and myvar is shared with all instances of myclass. That was the only test I have made before sending my prior message to the listing, and it led me to a wrong conclusion. But if there are any class parameters, class myclass mypar = let myvar = ... in object ... end then the class constructor is not constant. It is a function and should be applied to an argument to create an object. In this case the let is evaluated only when the class constructor is aplied to some arguments to build an instance of the class. This way the variable myvar cannot be shared by the instances of the class. I hope I got things right now. Regards. Romildo --ew6BAiZeqk4r7MaW Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="t1.ml" class c1 = let counter = ref 0 in object val mutable name = "" method set_name new_name = name <- new_name; method bump = counter := !counter + 1; print_string (name ^ " has counter = "); print_int (!counter); print_newline () end class c2 () = let counter = ref 0 in object val mutable name = "" method set_name new_name = name <- new_name; method bump = counter := !counter + 1; print_string (name ^ " has counter = "); print_int (!counter); print_newline () end let main () = let x1 = new c1 in let y1 = new c1 in let x2 = new c2 () in let y2 = new c2 () in x1#set_name "x1"; y1#set_name "y1"; x2#set_name "x2"; y2#set_name "y2"; x1#bump; y1#bump; x1#bump; y1#bump; x2#bump; y2#bump; x2#bump; y2#bump let _ = main () --ew6BAiZeqk4r7MaW--