Path: utzoo!attcan!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!usc!apple!apple.com!susser From: susser@apple.com (Joshua Susser) Newsgroups: comp.lang.smalltalk Subject: Re: Smalltalk Implementation Summary Message-ID: <5802@internal.Apple.COM> Date: 15 Dec 89 18:01:11 GMT Sender: usenet@Apple.COM Organization: Apple Computer, Inc. (ATG) Lines: 42 References:<235311@<1989Dec6> <22600003@inmet> In article <22600003@inmet> rich@inmet.inmet.com writes: > Is this legal in Smalltalk? I know blocks are not true lambda closures. > Little Smalltalk generates an error when you execute the "test" method. > > test > self getAblock value > | > getAblock > ^ [ ^ 2 ] The environment a block executes in is called a BlockContext (BC from now on). Blocks themselves are not first-class objects, but BCs are. A BC has a reference back to the MethodContext (MC) where it was *created* - this is called the home context, and it is where code in a block goes to get variables declared in its enclosing scope. When you say "^" in a block, you are returning FROM THE HOME CONTEXT to the home context's sender context. This is why things like "ifTrue: [^self]" can work. When you send getABlock, you activate the method and create a MC. Then you create a BC which refers to its home context, the current MC. Then you return from the current MC. This can only be done once. So when you evaluate the block, and it tries to return from its home context AGAIN, you have serious problems. So why can you only return from a method once? I don't know, go ask Peter Deutsch. Actually, he may have fixed this in the current version of Smalltalk-80. It's been over a year since I've used Smalltalk-80, so I haven't kept up with their technology. One feature I've always missed in blocks was a local return construct. There are times when I want to bail out of a block in the middle, and return a value to the place that called the block. This would allow for some more flexible control stuctures like loop exits, continues, etc. -Joshua ------------------------------------------------------------------------------ Joshua Susser Apple Computer ,Inc. Object Percussionist Advanced Technology Group arpa: susser@apple.com 20525 Mariani Ave. MS 76-2D uucp: {sun,nsc,...}!apple!susser Cupertino, CA 95014 AppleLink: susser.j 408/974-6997 "I beat on objects."