Implementing Verified Collections using Irony Script.NET
Friday, March 28th, 2008This entry will start a series of posts about Script.NET language features.
The first feature I would like to introduce is Contracts and Mutanic objects. I will start with an example. It will implement a verified collection. The collection will satisfy following requirement: it should contain no more than 10 items.
In the example I will use a Stack of integers. (Stack<int> in C#):
stack = new Stack<|int|>();
I will override two functions of stack: Push and Pop. For this purpose I will use DataMutant. DataMutant is a class that is able to decorate any existing .NET class. Because Script.NET is dynamically typed language there is no need to care about function signatures. Moreover, Script.NET uses Late binding for searching object’s functions.
To create a DataMutant object I will use special language construct:
mObject=[Push->Push,PopCheck->Pop];
It will create an object with two fields Push and PopCheck. Because, these fields are function pointers (they implement IInvokable interface) it is possible to invoke them as well: mObject.Push(20).
One more step is to decorate original stack: mObject.Mutate(stack).
One more thing that should be mentioned is function’s contracts. They are represented by three conditions: pre-, post- and invariant. Their semantics is very simple: pre condition should be satisfied before function body is being executed. post conditions should be satisfied after function body execution. invariant condition should preserve the value between function execution.
All above said is summarized on the following code snippet. Further instructions are at the bottom of the post.
Code for this example
- 1. Open Code for this example and copy it
- 2. Open Script.NET
- 3. Paste the Code
- 4. Press “Evaluate”
- 5. Change the number 5 in for statement to 15
- 6. Run example once again
- 7. See the precondition exception




