[an error occurred while processing this directive]

Smalltalk-Blocks in Java: This page describes how to implement a Smalltalk like Boolean type and Smalltalk like blocks (closures) in Java. [] (Smalltalk-Blocks in Java closure closures), report, page 722084
https://www.purl.org/stefan_ram/pub/smalltalk-blocks-in-java (permalink) is the canonical URI of this page.
Stefan Ram

Smalltalk -Blocks and Closures in Java

Variables

Variables can not be referenced within an inner class, unless they are declared final.

Guy Steel, involved in the design of Java, reportedly wrote in this regard:
“Actually, the prototype implementation *did* allow non-final variables to be referenced from within inner classes. There was an outcry from *users*, complaining that they did not want this! The reason was interesting: in order to support such variables, it was necessary to heap-allocate them, and (at that time, at least) the average Java programmer was still pretty skittish about heap allocation and garbage collection and all that. They disapproved of the language performing heap allocation "under the table" when there was no occurrence of the "new" keyword in sight.
Maybe we could lift that restriction in the next few years...
I dunno.”
“One of the early design principles of Java was that heap allocation occurs if and only if a construct involving the "new" keyword is executed. Adding full-blown closures violated this design principle. (Dynamic class loading also violated the principle, but users seemed to be comfortable with that, perhaps because they believed that "once the computation got going" no more heap allocation would occur.)
Other features for Java release 1.5 will perform certain kinds of autoboxing, which also violates the design principle. This fact will make it easier to argue for restoring full-blown support for closures in the future.”
http://madbean.com/2003/mb2003-49/

The following program shows how a variable can be emulated using an indirection: A final variable "_" contains a reference to an object, which then contains a modifiable int-Variable "i".

Blocks.java
public class Main 
{ static class IntVariable { int i; } 
public static void main( String[] args ) 
{ final IntVariable _ = new IntVariable(); 
new Runnable(){ public void run(){ _.i = 22; }}.run(); 
new Runnable(){ public void run(){ System.out.print( _.i ); }}.run(); }}

System.out
22 

Blocks

The following program shows how to implement a boolean type and blocks the Smalltalk  way.

It implements the following “pseudo Smalltalk code”.

Blocks.pseudo
[ int i = 0; while( [ i < 3 ], [ int j = 0; print( i++, j++ )])]

This program does not use the inbuilt type "boolean" of Java anywhere, nor does it use the class "java.lang.Boolean".

Blocks.java
import java.util.concurrent.Callable;
abstract class Boolean_ 
{ abstract void if_( Runnable statement );  
static Boolean_ true_ = new True();  
static Boolean_ false_ = new False(); }
class True extends Boolean_ 
{ public void if_( final Runnable statement ) 
{ statement.run(); }}
class False extends Boolean_ 
{ public void if_( final Runnable statement ){}}
class PairOf$$int$$And$$int 
{ final int left; final int right; 
final Boolean_[] converter;
PairOf$$int$$And$$int( final int left, final int right ) 
{ this.left = left; this.right = right;  
converter = new Boolean_[ 2 ]; 
converter[ 0 ]= Boolean_.false_; 
converter[ 1 ]= Boolean_.true_; }
Boolean_ isLessThan() 
{ return this.converter[ -(( this.left - this.right )>> 31 )]; }}
public class Main
{ static void while_ 
( final Callable<Boolean_> assertion, final Runnable statement ) 
{ try 
{ assertion.call().if_ 
( new Runnable() 
{ public void run() 
{ statement.run();  
while_( assertion, statement ); }}); } 
catch( Exception e ){ throw new RuntimeException( e ); }}
public static void main( final java.lang.String[] _ ) 
{ new Runnable() 
{ int i; 
public void run(){ i = 0; 
while_ 
( new Callable<Boolean_>()  
{ public Boolean_ call()  
{ return new PairOf$$int$$And$$int( i, 3 ).isLessThan(); }}, 
new Runnable() 
{ int j;  
public void run() 
{ j = 0; System.out.println( i++ ); 
System.out.println( j++ ); }}); }}.run(); }
}

System.out





0

Reddit   |   About this page, Impressum  |   Form for messages to the publisher regarding this page  |   "ram@zedat.fu-berlin.de" (without the quotation marks) is the email-address of Stefan Ram.   |   Beginning at the start page often more information about the topics of this page can be found. (A link to the start page appears at the very top of this page.)  |   Copyright 2004 Stefan Ram, Berlin. All rights reserved. This page is a publication by Stefan Ram. slrprd, PbclevtugFgrsnaEnz