Using this library we can express the arithmetic expression ''{{code|1 + 2''}} as we did in ''{{code|ExampleOne.AddOneAndTwo()''}} and can evaluate the expression by calling ''{{code|.Eval()''}}. Now imagine that we wish to extend this library, adding a new type is easy because we are working with an [[Object-oriented programming|Object-oriented programming language]]. For example, we might create the following class:
<syntaxhighlight lang="c#" line="1">
Line 194:
</syntaxhighlight>
However, if we wish to add a new function over the type (a new method in C# terminology) we have to change the ''{{code|IEvalExp''}} interface and then modify all the classes that implement the interface. Another possibility is to create a new interface that extends the ''{{code|IEvalExp''}} interface and then create sub-types for ''{{code|Lit''}}, ''{{code|Add''}} and ''{{code|Mult''}} classes, but the expression returned in ''{{code|ExampleOne.AddOneAndTwo()''}} has already been compiled so we will not be able to use the new function over the old type. The problem is reversed in functional programming languages like [[F Sharp (programming language)|F#]] where it is easy to add a function over a given type, but extending or adding types is difficult.
=== Problem Solution using Object Algebra ===
Line 225:
</syntaxhighlight>
We use the same implementation as in the first code example but now add a new interface containing the functions over the type as well as a factory for the algebra. Notice that we now generate the expression in ''{{code|ExampleTwo.AddOneToTwo()''}} using the ''{{code|ExpAlgebra<T>''}} interface instead of directly from the types. We can now add a function by extending the ''{{code|ExpAlgebra<T>''}} interface, we will add functionality to print the expression:
<syntaxhighlight lang="c#" line="1">
Line 286:
</syntaxhighlight>
Notice that in ''{{code|ExampleThree.Print()''}} we are printing an expression that was already compiled in ''{{code|ExampleTwo''}}, we did not need to modify any existing code. Notice also that this is still strongly typed, we do not need reflection or casting. If we would replace the ''{{code|PrintFactory()''}} with the ''{{code|ExpFactory()''}} in the ''{{code|ExampleThree.Print()''}} we would get a compilation error since the ''{{code|.Print()''}} method does not exist in that context.