-
Notifications
You must be signed in to change notification settings - Fork 99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix: Support self application of Buffer.append
#165
base: master
Are you sure you want to change the base?
Conversation
Debug.print "append test 1: cloning avoids the issue"; | ||
d.append(d.clone()); | ||
Debug.print "append test 2: cloning not necessary"; | ||
c.append(c); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we want to allow this? The result is tied closely (maybe too close) to the evaluation order, e.g. what's the result of c.append(c.append(c)).append(c)
. The clone()
approach makes this explicit, which is less likely to introduce bugs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, so you dislike option 3. I can understand why.
So, do you want to choose options 1 or 2 (see my list)? Or add other design option?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think option 2 makes sense. They can always clone the buffer if they want to self append. I would expect append
to be associative. Self append breaks that rule.
@@ -97,8 +97,9 @@ public class Buffer<X> (initCapacity : Nat) { | |||
/// Returns an [Iter](Iter.html#type.Iter) over the elements of this buffer. | |||
public func vals() : { next : () -> ?X } = object { | |||
var pos = 0; | |||
var valsCount = count; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let
?
d52aecd
to
08507fc
Compare
Appending a buffer with itself causes divergence, which may surprise or disappoint some developers.
Possible fixes:
Buffer.append
Buffer
's internal variables (same outcome as first cloning arg before using it for appending, but more efficient).This PR uses solution 3.
Rationale: The current "problem" is that there's a read-write dependency between the buffer's size when appearing as an argument, and when (also) appearing as the receiver of the
append
invocation. To permit this call with the expected functional behavior, we just need to break this dependency (copying the number of buffer elements to read before increasing this variable). This PR does that with a minor remedy to the code.