[e-lang] simple Guard example

Martin Scheffler martinscheffler at googlemail.com
Mon Jan 8 07:46:46 CST 2007


I tried to create a simple example of how to create guards.
The guard is supposed to guard objects created by a specific maker.

It basically checks if the guarded object posesses a sealer that fits
to the unsealer of the guard.

Could you take a look at this?
If this code is correct (and there is not a much simpler version of
how to do it), I would like to place it in the wiki at


def makeBrandPair := <elib:sealing.makeBrand>
# value: <makeBrand>

def makeCertifiedMaker() :any{

       #create a sealer/unsealer pair
       def [certifiedSealer, certifiedUnsealer] := makeBrandPair("certified")

       def certifiedMaker {

               #This guard throws an error if an uncertified object is
               to getGuard() :any {
                       def CertifiedGuard {
                               to coerce( specimen, ejector ) :any{

                                       def someObj := {}
                                       def sealed := specimen.seal( someObj )
                                       try {

certifiedUnsealer.unseal( sealed )
                                       } catch error {
                                               throw.eject( ejector,
"This is not a certified object!" )
                                               return null
                                       return specimen
                       return CertifiedGuard

               to run() :any {
                       def certified{
                               to print() :void{println("I am a
certified object!")}
                               to seal(obj) :any {return
                       return certified
       return certifiedMaker
# value: <makeCertifiedMaker>

def certifiedMaker:=makeCertifiedMaker()
# value: <certifiedMaker>

def Certified:=certifiedMaker.getGuard()
# value: <CertifiedGuard>

def myCertified:=certifiedMaker()
# value: <certified>

def test :Certified :=myCertified
# value: <certified>

#stdout: I am a certified object!

def [fakeSealer, fakeUnsealer] := makeBrandPair("certified")
# value: [<certified sealer>, <certified unsealer>]

def fakeCertified{

       to print() :void{
           println("HAHA, i sneaked in!")

       to seal(obj) :any {
           return fakeSealer.seal(obj)
# value: <fakeCertified>

def test2 :Certified :=fakeCertified
# problem: This is not a certified object!
#   - Thrower#eject(OneArgFunc, RuntimeException)
# (...)


More information about the e-lang mailing list