You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Using the with statement (as per example below), it follows (likely by language design) that p1.q2 equals p2.q2
Nonetheless the code clearly contains the implication that variable q2 should always be consistent with j1 (p1.q2 equals 31 and p2.q2 equals 45; Should this be flagged as a language warning? Or should readonly fields/properties that only depend on the constructor be moved. . . into the constructor. Changing q2 to a lambda fixes the problem but defeats the point of improving code efficiency.
test p1 = new(3, 5);
test p2 = p1 with { j1 = 10 };
public record test(int j1, int j2)
{
private readonly int q2 = 2 * j1 + j2 * j2;
public (int, int) Method(int j3)
{
return (q2 + j2 * j3, q2 * j3 + j2);
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Using the with statement (as per example below), it follows (likely by language design) that p1.q2 equals p2.q2
Nonetheless the code clearly contains the implication that variable q2 should always be consistent with j1 (p1.q2 equals 31 and p2.q2 equals 45; Should this be flagged as a language warning? Or should readonly fields/properties that only depend on the constructor be moved. . . into the constructor. Changing q2 to a lambda fixes the problem but defeats the point of improving code efficiency.
public record test(int j1, int j2)
{
private readonly int q2 = 2 * j1 + j2 * j2;
public (int, int) Method(int j3)
{
return (q2 + j2 * j3, q2 * j3 + j2);
}
}
Beta Was this translation helpful? Give feedback.
All reactions