Electric Communities: E Programming Language 

Hello ewhen

This tutorial demonstrates the ewhen statement. Since ewhen defers execution of code until a requested value has been supplied, you can use this statement to:

Hello E-When

To run this tutorial, enter the following code and study the walkthrough that accompanies it. This example is also available in the online example suite as HelloEWhen.e

  Tutorial for ewhen statement
  Copyright 1996 Electric Communities, all rights reserved.

  Program output:

  Sent StartHelloEWhenInstance
  In HelloEWhenObj
  In HelloEWhenObj2
  Hello World with 7


import ec.e.lang.EInteger;

public class HelloEWhen
  public static void main(String args[]) {  
    HelloWorldEWhenObj theInstance = new HelloWorldEWhenObj();
    theInstance <- StartHelloEWhenInstance();
    System.out.println("Sent StartHelloEWhenInstance");

eclass HelloWorldEWhenObj
  EInteger anEInteger;
  emethod StartHelloEWhenInstance() {
    HelloEWhenObj2 first = new HelloEWhenObj2();
    System.out.println("In HelloEWhenObj ");
    first <- hello(&anEInteger);
    ewhen anEInteger (int thisInt) {
      System.out.println("Hello World with " + thisInt);

eclass HelloEWhenObj2 
  emethod hello(EDistributor putEIntHere) {
    System.out.println("In HelloEWhenObj2");
    putEIntHere <- forward( new EInteger(7) );

The Walkthrough

  1. main instantiates the E-object theInstance, an instance of E-class HelloWorldEWhenObj.
  2. main sends the message StartHelloEWhenInstance to theInstance.
  3. StartHelloEWhenInstance sends the message hello to first, passing as an argument the distributor for the channel representing the uninitialized instance variable anEInteger.
  4. StartHelloEWhenInstance executes an ewhen statement that says, ``When you get a value for the instance variable anEInteger, assign it to thisInt and execute the following code block.'' StartHelloEWhenInstance waits for the value to be defined.
  5. Meanwhile, first executes the HelloEWhenObj2 E-method hello to handle the message sent to it.
  6. hello instantiates a new EInteger with a value of 7 and forwards the specified distributor's channel to point to this value.
  7. StartHelloEWhenInstance's ewhen statement detects that an EInteger is now defined, assigns its value to thisInt, and prints out, ``Hello World with 7''.


You have used ewhen to implement optimistic computation, since the ewhen code execution was deferred until the required instance variable was initialized. This code would not work in another object-oriented language, since that language would think the code was incomplete (because of the uninitialized variables). Using E you don't have to make assumptions about network latency; your code will simply complete as much as it can.

Generally, you should also include timeouts (see the E Programmer's Manual section on time-outs) to prevent your code from simply waiting until it gets the requested value. In this exercise you're creating this code by yourself on one machine, but in practice, you would be working with other objects on other machines.

Compiling and Running

To compile source code for the E runtime, use the ecomp compiler:

  ecomp filename

The ecomp command compiles E and Java source files directly into Java bytecodes. You can then run your compiled program with the E Java interpreter (the javaec command):

  javaec filename

For more information on these commands, see the E Tools and Utilities section in the E Programmer's Guide.

Copyright (c) 1996 Electric Communities. All rights reserved worldwide.
Most recent update: 5/29/96