[ioke-scm] [ioke~main:57058584] Add support for passNil

  • From: olabini@kenai.com
  • To: commits@ioke.kenai.com
  • Subject: [ioke-scm] [ioke~main:57058584] Add support for passNil
  • Date: Thu, 28 Jan 2010 15:11:33 +0000

Project:    ioke
Repository: main
Revision:   57058584d58e51635377153e0caed6b394de1ac0
Author:     olabini
Date:       2010-01-28 15:11:04 UTC
Link:       

Log Message:
------------
Add support for passNil


Revisions:
----------
57058584d58e51635377153e0caed6b394de1ac0


Modified Paths:
---------------
src/builtin/M05_ignoreErrors.ik
test/default_behavior_spec.ik


Diffs:
------
diff --git a/src/builtin/M05_ignoreErrors.ik b/src/builtin/M05_ignoreErrors.ik
index 1220ca6..287858f 100644
--- a/src/builtin/M05_ignoreErrors.ik
+++ b/src/builtin/M05_ignoreErrors.ik
@@ -24,3 +24,23 @@ return the result of the first evaluation",
   [protectedCode, otherwiseCode]
   bind(rescue(self, fn(c, otherwiseCode evaluateOn(call ground, call 
ground))),
     protectedCode evaluateOn(call ground, call ground)))
+
+
+DefaultBehavior FlowControl passNil = dmacro(
+  [code]
+
+  currentMessage  = code
+  ground = call ground
+  currentReceiver = call ground
+
+  while(currentMessage,
+    currentReceiver = currentMessage sendTo(currentReceiver, ground)
+    if(currentReceiver nil?,
+      return(nil))
+    currentMessage = currentMessage next
+    if(currentMessage && currentMessage terminator?,
+      currentMessage = currentMessage next
+      currentReceiver = ground)
+  )
+  currentReceiver
+)
diff --git a/test/default_behavior_spec.ik b/test/default_behavior_spec.ik
index d235abd..99e2968 100644
--- a/test/default_behavior_spec.ik
+++ b/test/default_behavior_spec.ik
@@ -1117,4 +1117,71 @@ describe(DefaultBehavior,
       ''(mux ''(abc)) code should == "mux '(abc)"
     )
   )
+
+  describe("passNil",
+    it("should just return nil when given nil",
+      passNil(nil) should be nil
+    )
+
+    it("should not call any methods after a nil has been returned",
+      x = Origin mimic
+      y = Origin mimic
+      z = Origin mimic
+
+      x toReturn = y
+      z meCalled = false
+      x foo = method(@meCalled = true. @toReturn)
+
+      y toReturn = nil
+      z meCalled = false
+      y bar = method(@meCalled = true. @toReturn)
+
+      z meCalled = false
+      z blarg = method(@meCalled = true)
+
+      passNil(x foo bar blarg flux mox moo) should be nil
+
+      x meCalled should be true
+      y meCalled should be true
+      z meCalled should be false
+    )
+
+    it("should not send any more messages after a nil has been found",
+      x = Origin mimic
+      y = Origin mimic
+
+      x foo = y
+      y bar = nil
+
+      passNil(x foo bar blarg flux mox moo) should be nil
+    )
+
+    it("should return the last value returned from the message chain",
+      x = Origin mimic
+      y = Origin mimic
+
+      x foo = y
+      y bar = 42
+
+      passNil(x foo bar) should == 42
+    )
+
+    it("should execute in the outside context",
+      x = Origin mimic
+      x foo = method(val, val)
+      y = Origin mimic
+
+      passNil(x foo(y)) should be same(y)
+    )
+
+    it("should allow terminators in the chain",
+      x = Origin mimic
+      y = Origin mimic
+
+      x foo = y
+      y bar = 42
+
+      passNil(x foo. x foo bar) should == 42
+    )
+  )
 )




[ioke-scm] [ioke~main:57058584] Add support for passNil

olabini 01/28/2010
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2010, Oracle Corporation and/or its affiliates
(revision 20120127.ac94057)
 
 
Close
loading
Please Confirm
Close