ERights Home elang 
Back to: E Language Tutorial x On to: Grammar and Expansions

E Idioms
Quick Reference

mostly by Marc Stiegler


This is intended as a compact representation of frequently-used syntax and frequently-encountered special characteristics of E. The beginning E user may find it a handy reference while experimenting and reading the book; the experienced E programmer may find it handy as a refresher if returning to the language after some time. This reference does not touch quasi-literals, regular expressions, pattern matching, parse trees, or Kernel-E at all.


Simple Statements

# set pragmas to the version of E syntax being used
pragma.syntax("0.9")

def a := 2 + 3
var a2 := 4
a2 += 1
def b := "answer: "
println(b + a)
# Note: the plus sign is interpreted by the object on the left, so b as a string
# causes plus to mean string concatenation. The statement "b + a"
# creates a concatenated string without explicit conversion; "a + b" would fail
# as it tried to cast string b to an integer for addition to a.

Basic Flow

if (a == b) {
    println("match")
} else {
    println("no match")
}
while (a < b) {
    a += 1
}
try {
    3 // 0
} catch err {
    println("error: " + err)
} finally {
    println("always")
}
for next in 1..3 {
    println(next)
}
for key => value in map {
    println("got pair")
}

Modules

Function Singleton Object (stateless)
def addTwoPrint(number) {
    println(number + 2)
    return number + 2
}

def twoPlusThree := addTwoPrint(3)
def adder {
    to add1(number) {return number + 1}
    to add2(number) {return number + 2}
}
def result := adder.add1(3)
Objects with state Objects self-referencing during construction
def makeOperator(baseNum) {
    def instanceValue := 3
    def operator {
        to addBase(number) {
            return baseNum + number
        }
        to multiplyBase(number) {
            return baseNum * number
        }
    }
    return operator
}
def threeHandler := makeOperator(3)
def threeTimes2 := threeHandler.multiplyBase(2)
def makeOp() {
    def op
    def myAlerter := makeAlerter(op)
    bind op {
        to respondToAlert() {
            myAlerter("got alert")
        }
    }
    return op
}
Delegation Matching an Interface like a Java Adapter
def makeExtendedFile(myFile) {
    def extendedFile extends myFile {
        to append(text) {
            var current := myFile.getText()
            current := current + text
            myFile.setText(current)
        }
    }
    return extendedFile
}
# "extends" keyword automatically delegates
def makeUpListener(reactor) :any {
    def upListener {
        to mouseUp(event) {
            reactor.mouseUp()
        }
        match [verb, args] {}
    }
    return upListener
}
# upListener meets the Java MouseListener interface

Text File I/O Windowed Applications

Text files are always normalized to use linefeed ('\n') as end of line when read in to the program; the end of line is converted to native format upon being written out. File paths can always use "/" to separate subdirectories

Tilda (~) is a reference to home directory, with meaning in both MSWindows and Linux.
    def fileA := <file:~/Desktop/text.txt>
    def fileB := <file>["/home/marcs/text.txt"]

# Note the blank space between the colon and the quote above

    def fileC := <c:/windows/desktop/text.txt>
    fileA.setText("abc")
    def contents := fileA.getText()
    for line in contents.split("\n") {
        println(line)
    } 
# label A consumes horizontal space,
# textArea consumes vertical space and
# space beneath both labelA and labelB

def panel :=
JPanel`$labelA.X   $labelB
       $textArea.Y >`

# build app
# display windows
# in window listener on
# WindowClosing event:

interp.continueAtTop()

# at end of program, after
# construction and window opening

interp.blockAtTop()

Data Structures

ConstList ConstMap
var a := [8,6,"a"]

# a[2] == "a" is true
# a.size() == 3 is true 

for i in a { println(i) }
a := a + ["b"]

# a(0,2) == [8,6] is true

def flexA := a.diverge()
def m := ["c" => 5]

# m["c"] == 5 is true
# m.size() == 1 is true

for key => value in m {
    println(value)
}
def flexM := m.diverge()
FlexList FlexMap
flexA.append(["b"])
flexA.push("b")
def constA := flexA.snapshot()
flexM["b"] := 2
flexM.removeKey("b")
def constM := flexM.snapshot()

Strings are ConstLists of char that have additional methods including Java string operators.
Flex structures respond to all Const messages except the comparison operators ("<", <=", etc...).
All these structures have many methods not listed here.


Java Interface

# we can directly name awt and swing classes using URI syntax

    def frame1 := <awt:Frame>()
    def frame2 := <swing:JFrame>()
    def panel := <swing:JPanel>()
    E.call(frame2.getContentPane(), "add(Component)", [panel])

# E.call() needed when a method name in Java is overloaded with multiple
# methods with the same number of arguments, different types, and the different
# types are super/sub classes of one another

    def byte := <import:java.lang.Byte>.asType()
    def byteArray := byte[300]

# creates a Java array of primitive objects,
# byte, char, int, etc. 

emakers
def uiKit := <import:com.skyhunter.ex.uiKit>
def buttonFunc() { println("pressed button") }
def button := uiKit.newToolButton(iconImage, "tip", buttonFunc)
def makeDialogVow := <import:com.skyhunter.ex.swing.dialogVowMakerAuthor>(<swing:JFrame>)
def helpDialog := makeDialogVow("Help",
                                "<html><b>Help</b><p>This is help.</html>",
                                null,
                                ["OK"])

Eventual Sends
abacus <- add(a,b)
when (def answer := abacus <- add(a,b)) -> {
    println(`computation complete:$answer`)
} catch problem {
    println(`promise broken $problem`)
} finally {
    println("always")
}
def carRcvr := makeCarRcvr <- ("Mercedes")
Ref.whenBroken(carRcvr, def lost(brokenRef) {
    println("Lost connection to carRcvr")
})
def [resultVow, resolver] := Ref.promise()
when (resultVow) -> {
    println(resultVow)
} catch prob {
    println(`oops: $prob`)
}
resolver.resolve("this text is the answer")

Remote Comm

Initialization
introducer.onTheAir()              
def makeURI(obj) {
    return introducer.sturdyToURI(sturdyRef.temp(obj))
} 
def makeFromURI(uri) {
    return introducer.sturdyFromURI(uri).getRcvr()
} 
 
Unless stated otherwise, all text on this page which is either unattributed or by Mark S. Miller is hereby placed in the public domain.
ERights Home elang 
Back to: E Language Tutorial x On to: Grammar and Expansions
Download    FAQ    API    Mail Archive    Donate

report bug (including invalid html)

Golden Key Campaign Blue Ribbon Campaign