This is a great question, Abdelghani. It comes down to the meaning of the phrase "everything is an object". When you wrote "currentBloc value", that caused the block *[ 1 to:50 do: [:index |Transcript show: index. 100 milliSeconds asDelay wait]. Transcript cr. 1000 milliSeconds asDelay wait ]* to evaluate. The fact that you hold a reference to that block elsewhere and change what object is referenced at some point does not change the fact that the block itself is executing.
Until your code executes *currentBloc value* again, it won't start the new block executing. Imagine if I were to hand you a loaf of bread and tell you to feed it to the dog. If I were then to hand you another loaf of bread, the dog would continue eating the first loaf. We never told the dog to do anything else. On Mon, Oct 23, 2017 at 1:53 PM, abdelghani ALIDRA via Pharo-users < pharo-users@lists.pharo.org> wrote: > > > ---------- Forwarded message ---------- > From: abdelghani ALIDRA <alidran...@yahoo.fr> > To: <pharo-users@lists.pharo.org> > Cc: > Bcc: > Date: Mon, 23 Oct 2017 20:53:51 +0000 (UTC) > Subject: Dynamically changing code in a safe way > Hi, > > If I create a class HotSwapping with the instance variable currentBLoc > (and the corresponding accessors) and the following methods : > > HotSwapping>>initialize > currentBloc := [ 1 to:50 do: [:index |Transcript show: index. 100 > milliSeconds asDelay wait]. Transcript cr. 1000 milliSeconds asDelay wait ] > > HotSwapping>>run > [10 timesRepeat: [ currentBloc value ]] fork > > I then do : obj := HotSwapping new run > The “funny” thing is that, when Transcript starts showing numbers, lets > say : 12345678910 (i.e. the bloc is in the middle of its execution), I do > obj currentBloc: [Transcript show: ‘Hello’;cr. 1000 milliSeconds asDelay > wait ] > > The Transcript does not stop showing numbers. It shows the Hello message > only after the old bloc has finished executing (the entire line of number > form 0 to 50 has been printed in the Transcript) > > So why does not this happen this way? Why the assignment of the new code > is delayed until the end of the bloc execution? is it something related to > threads execution or does Pharo implement some safety hot swapping > mechanisms? > > Thanks in advance. > Abdelghani > > >