Git Product home page Git Product logo

Comments (4)

GoogleCodeExporter avatar GoogleCodeExporter commented on September 28, 2024
I think it's reasonable to iterate over a List<T> twice, so we could run over 
the sequence to check for nulls in that case, just before calling list.AddRange.

Alternatively, we could build a List<T> from the source to start with if it 
wasn't already a list, check everything and then call AddRange in all cases. 
The advantage of this would be that if we were going to throw an exception, 
we'd do so *before* adding anything to the target. It does mean creating an 
extra list, admittedly...

Original comment by jonathan.skeet on 12 Sep 2011 at 5:26

from protobuf-csharp-port.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 28, 2024
The interesting thing here is that we always wind up in the else clause...

            else
            {
                foreach (T element in source)
                {
                    destination.Add(element);
                }
            }

... since the target list is never a List<T>, but our own PopsicleList<T>.  If 
we are going to foreach through the array we can test elements for null at that 
time.

I would recommend we move the null assertion to inside the PopsicleList.  
PopsicleList's current Add() and this[] {set;} methods do not assert that the 
value is non-null.  So I can still break it with the following:

    MyMessage.Builder b;
    b.RepeatedValueList.Add(null);


I'll put together a fix branch for this, I think this is critical enough given 
today's world of Linq and yield statements.

Original comment by [email protected] on 16 Sep 2011 at 3:26

  • Changed state: Accepted
  • Added labels: Priority-High
  • Removed labels: Priority-Medium

from protobuf-csharp-port.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 28, 2024
Fixed on branch 'issue-26', ready for review.  The change involved a few 
things....

1. Removed protected member "AddRange<T>" of GeneratedBuilder
2. Generator now calls PopsicleList<T>.Add(IEnumerable<T>)
3. PopsicleList now checks for null on all add/set/insert methods
4. The double-enumeration now only happens if the provided IEnumerable<T> also 
implements ICollection<T>

            bool CheckForNull = default(T) == null;
            ...
            if (!CheckForNull || collection is PopsicleList<T>)
            {
                items.AddRange(collection);
            }
            else
            {
                // Assumption, it's ok to enumerate collections more than once.
                if (collection is ICollection<T>)
                {
                    ThrowHelper.ThrowIfAnyNull(collection);
                    items.AddRange(collection);
                }
                else
                {
                    foreach (T item in collection)
                    {
                        ThrowHelper.ThrowIfNull(item);
                        items.Add(item);
                    }
                }
            }

Original comment by [email protected] on 16 Sep 2011 at 5:28

  • Changed state: Started

from protobuf-csharp-port.

GoogleCodeExporter avatar GoogleCodeExporter commented on September 28, 2024

Original comment by [email protected] on 16 Sep 2011 at 6:29

  • Changed state: Fixed

from protobuf-csharp-port.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.