Interface IRewritable<T>
A object is rewriterable if it knows how to access its immediate children.
Implementations should ensure that you always get the children you just set
(rewritable.SetChildren(children).GetChildren() == children
),
and that successive sets overwrite the earlier operation
(rewritable.SetChildren(children1).SetChildren(children2) == rewritable.SetChildren(children2)
).
Namespace: Sawmill
Assembly: Sawmill.dll
Syntax
public interface IRewritable<T>
where T : IRewritable<T>
Type Parameters
Name | Description |
---|---|
T | The type of the object implementing the interface |
Methods
| Improve this Doc View SourceGetChildren()
Get the immediate children of the current instance. GetChildren(T)
Declaration
Children<T> GetChildren()
Returns
Type | Description |
---|---|
Children<T> | The immediate children of the current instance |
Examples
Given a representation of the expression (1+2)+3
,
Expr expr = new Add(
new Add(
new Lit(1),
new Lit(2)
),
new Lit(3)
);
GetChildren() returns the immediate children of the topmost node.
Expr[] expected = new[]
{
new Add(
new Lit(1),
new Lit(2)
),
new Lit(3)
};
Assert.Equal(expected, expr.GetChildren());
|
Improve this Doc
View Source
RewriteChildren(Func<T, T>)
Update the immediate children of the current instance by applying a transformation function to each one.
Implementations of IRewritable<T> can use DefaultRewriteChildren<T>(T, Func<T, T>), or you can write your own.
NB: A hand-written implementation will not usually be faster than DefaultRewriteChildren<T>(T, Func<T, T>). If your type has a fixed number of children, and that number is greater than two, you may see some performance improvements from implementing this method yourself. Be careful not to rebuild the object if none of the children have changed.
RewriteChildren(Func<T, T>, T)Declaration
T RewriteChildren(Func<T, T> transformer)
Parameters
Type | Name | Description |
---|---|---|
Func<T, T> | transformer | A transformation function to apply to each of the current instance's immediate children. |
Returns
Type | Description |
---|---|
T | A copy of the current instance with updated children. |
Examples
Given a representation of the expression (1+2)+3
,
Expr expr = new Add(
new Add(
new Lit(1),
new Lit(2)
),
new Lit(3)
);
RewriteChildren(Func<T, T>) only affects the immediate children of the topmost node.
Expr expected = new Add(
transformer(new Add(
new Lit(1),
new Lit(2)
)),
transformer(new Lit(3))
);
Assert.Equal(expected, expr.RewriteChildren(transformer));
|
Improve this Doc
View Source
SetChildren(Children<T>)
Set the immediate children of the currentInstance.
Callers should ensure that newChildren
contains the same number of children as was returned by
GetChildren().
Declaration
T SetChildren(Children<T> newChildren)
Parameters
Type | Name | Description |
---|---|---|
Children<T> | newChildren | The new children |
Returns
Type | Description |
---|---|
T | A copy of the current instance with updated children. |
Examples
Given a representation of the expression (1+2)+3
,
Expr expr = new Add(
new Add(
new Lit(1),
new Lit(2)
),
new Lit(3)
);
SetChildren(Children<T>) replaces the immediate children of the topmost node.
Expr expected = new Add(
new Lit(4),
new Lit(5)
);
Assert.Equal(expected, expr.SetChildren(Children.Two(new Lit(4), new Lit(5))));