Skip to content

Commit 85070d6

Browse files
authored
Make ExpressionTree readonly struct (#266)
1 parent 729b734 commit 85070d6

File tree

2 files changed

+24
-55
lines changed

2 files changed

+24
-55
lines changed

Orm/Xtensive.Orm.Tests.Core/Linq/ExpressionTreeTest.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,20 +102,20 @@ public void NullEqualityTest()
102102
Expression<Func<int, int>> expression = x => x + 1;
103103
var t = expression.ToExpressionTree();
104104

105-
Assert.That(!t.Equals(null));
105+
Assert.That(!t.Equals(default));
106106

107-
Assert.That(null!=t);
108-
Assert.That(t!=null);
107+
Assert.That(default!=t);
108+
Assert.That(t!=default);
109109

110-
Assert.That(!(t==null));
111-
Assert.That(!(null==t));
110+
Assert.That(!(t==default));
111+
Assert.That(!(default==t));
112112

113-
t = null;
113+
t = default;
114114

115-
Assert.That(t==null);
116-
Assert.That(null==t);
117-
Assert.That(!(t!=null));
118-
Assert.That(!(null!=t));
115+
Assert.That(t==default);
116+
Assert.That(default==t);
117+
Assert.That(!(t!=default));
118+
Assert.That(!(default!=t));
119119
}
120120

121121
private static Expression<Func<int, int, int>> CreateProduct()
@@ -133,4 +133,4 @@ private static Expression<Func<int, int, int>> CreateSum2()
133133
return (x, y) => x + y;
134134
}
135135
}
136-
}
136+
}

Orm/Xtensive.Orm/Linq/ExpressionTree.cs

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,7 @@ namespace Xtensive.Linq
1616
/// that can be used for comparing expression trees and calculating their hash codes.
1717
/// </summary>
1818
[DebuggerDisplay("{expression}")]
19-
public sealed class ExpressionTree
20-
: IEquatable<ExpressionTree>
19+
public readonly struct ExpressionTree : IEquatable<ExpressionTree>
2120
{
2221
private readonly int hashCode;
2322
private readonly Expression expression;
@@ -26,38 +25,21 @@ public sealed class ExpressionTree
2625
/// Gets the underlying <see cref="Expression"/>.
2726
/// </summary>
2827
/// <returns></returns>
29-
public Expression ToExpression()
30-
{
31-
return expression;
32-
}
33-
28+
public Expression ToExpression() => expression;
29+
3430
#region ToString, GetHashCode, Equals, ==, != implementation
3531

3632
/// <inheritdoc/>
37-
public override string ToString()
38-
{
39-
return expression.ToString(true);
40-
}
33+
public override string ToString() => expression.ToString(true);
4134

4235
/// <inheritdoc/>
43-
public override int GetHashCode()
44-
{
45-
return hashCode;
46-
}
36+
public override int GetHashCode() => hashCode;
4737

4838
/// <inheritdoc/>
49-
public override bool Equals(object obj)
50-
{
51-
return Equals(obj as ExpressionTree);
52-
}
39+
public bool Equals(ExpressionTree other) => new ExpressionComparer().AreEqual(expression, other.expression);
5340

5441
/// <inheritdoc/>
55-
public bool Equals(ExpressionTree other)
56-
{
57-
if (ReferenceEquals(other, null))
58-
return false;
59-
return new ExpressionComparer().AreEqual(expression, other.expression);
60-
}
42+
public override bool Equals(object obj) => obj is ExpressionTree other && Equals(other);
6143

6244
/// <summary>
6345
/// Implements the operator ==.
@@ -67,11 +49,8 @@ public bool Equals(ExpressionTree other)
6749
/// <returns><see langword="true"/> if <paramref name="left"/> is equal to <paramref name="right"/>.
6850
/// Otherwise, <see langword="false"/>.
6951
/// </returns>
70-
public static bool operator == (ExpressionTree left, ExpressionTree right)
71-
{
72-
return Equals(left, right);
73-
}
74-
52+
public static bool operator == (ExpressionTree left, ExpressionTree right) => Equals(left, right);
53+
7554
/// <summary>
7655
/// Implements the operator !=.
7756
/// </summary>
@@ -80,10 +59,7 @@ public bool Equals(ExpressionTree other)
8059
/// <returns><see langword="true"/> if <paramref name="left"/> is not equal to <paramref name="right"/>.
8160
/// Otherwise, <see langword="false"/>.
8261
/// </returns>
83-
public static bool operator != (ExpressionTree left, ExpressionTree right)
84-
{
85-
return !Equals(left, right);
86-
}
62+
public static bool operator != (ExpressionTree left, ExpressionTree right) => !Equals(left, right);
8763

8864
#endregion
8965

@@ -94,29 +70,22 @@ public bool Equals(ExpressionTree other)
9470
/// <param name="right">Second expression to compare.</param>
9571
/// <returns>true, if <paramref name="left"/> and <paramref name="right"/>
9672
/// are equal by value, otherwise false.</returns>
97-
public static bool Equals(Expression left, Expression right)
98-
{
99-
return new ExpressionComparer().AreEqual(left, right);
100-
}
73+
public static bool Equals(Expression left, Expression right) => new ExpressionComparer().AreEqual(left, right);
10174

10275
/// <summary>
10376
/// Calculates hash code by value for the specified <paramref name="expression"/>.
10477
/// </summary>
10578
/// <param name="expression">Expression to calculate hash code for.</param>
10679
/// <returns>Hash code for <paramref name="expression"/>.</returns>
107-
public static int GetHashCode(Expression expression)
108-
{
109-
return new ExpressionHashCodeCalculator().CalculateHashCode(expression);
110-
}
80+
public static int GetHashCode(Expression expression) => new ExpressionHashCodeCalculator().CalculateHashCode(expression);
11181

11282

11383
// Constructors
11484

11585
internal ExpressionTree(Expression expression)
11686
{
117-
ArgumentValidator.EnsureArgumentNotNull(expression, "expression");
11887
this.expression = expression;
11988
hashCode = new ExpressionHashCodeCalculator().CalculateHashCode(expression);
12089
}
12190
}
122-
}
91+
}

0 commit comments

Comments
 (0)