<!-- 
RSS generated by JIRA (9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66) at Wed Feb 07 21:36:21 UTC 2024

It is possible to restrict the fields that are returned in this document by specifying the 'field' parameter in your request.
For example, to request only the issue key and summary append 'field=key&field=summary' to the URL of your request.
-->
<rss version="0.92" >
<channel>
    <title>MongoDB Jira</title>
    <link>https://jira.mongodb.org</link>
    <description>This file is an XML representation of an issue</description>
    <language>en-us</language>    <build-info>
        <version>9.7.1</version>
        <build-number>970001</build-number>
        <build-date>13-04-2023</build-date>
    </build-info>


<item>
            <title>[CSHARP-280] Compiler error: ambiguous call between IEnumerable&lt;MongoDB.Bson.BsonValue&gt; and IEnumerable&lt;object&gt; in BsonArray</title>
                <link>https://jira.mongodb.org/browse/CSHARP-280</link>
                <project id="10041" key="CSHARP">C# Driver</project>
                    <description>&lt;p&gt;*) Build environment:&lt;br/&gt;
VS 2010, Target platform: .NET 3.5, driver versions: 1.1 both official and the latest sources&lt;/p&gt;

&lt;p&gt;*) Steps to reproduce:&lt;br/&gt;
Add the following method to the MongoDB.BsonUnitTests.BsonArrayTests:&lt;/p&gt;

&lt;p&gt;		void TestCompilerError()&lt;br/&gt;
		{&lt;br/&gt;
			var values = new BsonValue[] { };&lt;/p&gt;

&lt;p&gt;			// all 3 calls cannot compile&lt;br/&gt;
			BsonArray.Create(values);&lt;br/&gt;
			var array = new BsonArray(values);&lt;br/&gt;
			array.AddRange(values);&lt;br/&gt;
		}&lt;/p&gt;

&lt;p&gt;.. and compile. Compilation fails with an error (3 similar errors):&lt;/p&gt;

&lt;p&gt;The call is ambiguous between the following methods or properties: &apos;MongoDB.Bson.BsonArray.Create(System.Collections.Generic.IEnumerable&amp;lt;MongoDB.Bson.BsonValue&amp;gt;)&apos; and &apos;MongoDB.Bson.BsonArray.Create(System.Collections.Generic.IEnumerable&amp;lt;object&amp;gt;)&apos;&lt;/p&gt;

&lt;p&gt;*) Possible solution:&lt;br/&gt;
Remove 3 methods dealing with System.Collections.Generic.IEnumerable&amp;lt;object&amp;gt; from MongoDB.Bson.BsonArray (constructor, Create, AddRange). As it was mentioned in &lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-276&quot; class=&quot;external-link&quot; rel=&quot;nofollow&quot;&gt;https://jira.mongodb.org/browse/CSHARP-276&lt;/a&gt;, these methods are redundant after adding methods dealing with non-generic IEnumerable. Now they seem to be also troublesome.&lt;/p&gt;

&lt;p&gt;I tried to remove these 3 methods from the latest sources. The test code is compiled successfully.&lt;/p&gt;</description>
                <environment></environment>
        <key id="20075">CSHARP-280</key>
            <summary>Compiler error: ambiguous call between IEnumerable&lt;MongoDB.Bson.BsonValue&gt; and IEnumerable&lt;object&gt; in BsonArray</summary>
                <type id="1" iconUrl="https://jira.mongodb.org/secure/viewavatar?size=xsmall&amp;avatarId=14703&amp;avatarType=issuetype">Bug</type>
                                            <priority id="3" iconUrl="https://jira.mongodb.org/images/icons/priorities/major.svg">Major - P3</priority>
                        <status id="6" iconUrl="https://jira.mongodb.org/images/icons/statuses/closed.png" description="The issue is considered finished, the resolution is correct. Issues which are closed can be reopened.">Closed</status>
                    <statusCategory id="3" key="done" colorName="success"/>
                                    <resolution id="9">Done</resolution>
                                        <assignee username="robert@mongodb.com">Robert Stam</assignee>
                                    <reporter username="nightroman">Roman Kuzmin</reporter>
                        <labels>
                            <label>driver</label>
                    </labels>
                <created>Mon, 25 Jul 2011 05:49:35 +0000</created>
                <updated>Thu, 2 Apr 2015 18:28:33 +0000</updated>
                            <resolved>Wed, 27 Jul 2011 19:46:47 +0000</resolved>
                                    <version>1.1</version>
                                    <fixVersion>1.2</fixVersion>
                                                        <votes>0</votes>
                                    <watches>0</watches>
                                                                                                                <comments>
                            <comment id="44919" author="rstam" created="Wed, 27 Jul 2011 19:46:47 +0000"  >&lt;p&gt;Rarely backward breaking at the source code level, but client code will have to be recompiled to pick up the new matching overloads in some cases.&lt;/p&gt;

&lt;p&gt;Note: there appear to be some similar ambiguities between IDictionary and IDictionary&amp;lt;string, object&amp;gt; but I will leave those for a separate JIRA ticket.&lt;/p&gt;</comment>
                            <comment id="44911" author="nightroman" created="Wed, 27 Jul 2011 19:14:23 +0000"  >&lt;p&gt;I am glad that you come to the same conclusion about replacing IEnumerable&amp;lt;object&amp;gt; in parameters with non-generic IEnumerable. As for the return values, yes, I prefer IEnumerable&amp;lt;object&amp;gt;, too, and this does not make any issues (as far as I know and can tell, neither in C# nor in PowerShell).&lt;/p&gt;</comment>
                            <comment id="44892" author="rstam" created="Wed, 27 Jul 2011 18:17:03 +0000"  >&lt;p&gt;The reason the call is ambiguous when .NET 3.5 is targeted is that in the absence of covariance there is no implicit conversion between IEnumerable&amp;lt;BsonValue&amp;gt; and IEnumerable&amp;lt;object&amp;gt;, so to the compiler both overloads are equally applicable and therefore ambiguous.&lt;/p&gt;

&lt;p&gt;In .NET 4 with the introduction of covariance there IS an implicit conversion from IEnumerable&amp;lt;BsonValue&amp;gt; to IEnumerable&amp;lt;object&amp;gt; but not the other way around, and that is enough to make the compiler decide that the overload with the more specific parameter type (i.e. IEnumerable&amp;lt;BsonValue&amp;gt;) is better and therefore when .NET 4 is targeted there no longer is any ambiguity.&lt;/p&gt;

&lt;p&gt;Furthermore, I agree with your analysis that wherever IEnumerable&amp;lt;object&amp;gt; is used (at least as an input parameter) we can use IEnumerable instead. I think for return values we can still stick to IEnumerable&amp;lt;object&amp;gt; where appropriate.&lt;/p&gt;</comment>
                            <comment id="44399" author="nightroman" created="Mon, 25 Jul 2011 22:42:01 +0000"  >&lt;p&gt;I propose the following (just like in &lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-276&quot; class=&quot;external-link&quot; rel=&quot;nofollow&quot;&gt;https://jira.mongodb.org/browse/CSHARP-276&lt;/a&gt;). Replace API methods dealing with IEnumerable&amp;lt;object&amp;gt; with non-generic methods dealing with IEnumerable (perhaps not only in BsonArray). Reasons:&lt;/p&gt;

&lt;p&gt;1) As we can see, non-generic methods open doors for PowerShell code;&lt;br/&gt;
2) Technically we lose nothing by this replacement, all methods that used to call X(IEnumerable&amp;lt;object&amp;gt;) can now call X(IEnumerable), at least after rebuild;&lt;br/&gt;
3) We can see that pairs X(IEnumerable) + X(IEnumerable&amp;lt;T&amp;gt;) in 3.5 are not ambiguous (unlike X(IEnumerable&amp;lt;object&amp;gt;) + X(IEnumerable&amp;lt;T&amp;gt;);&lt;br/&gt;
4) In 4.0 both ways do (old with X(IEnumerable&amp;lt;object&amp;gt; and new with non-generic IEnumerable).&lt;/p&gt;

&lt;p&gt;I think that in 3.5 methods X(IEnumerable&amp;lt;object&amp;gt;) should be avoided. They are not different by nature than X(IEnumerable) but they cannot be called with IEnumerable references. In contrast, non-generic methods can be called with IEnumerable&amp;lt;object&amp;gt;. Thus, API with non-generics gets more cases working fine in 3.5.&lt;/p&gt;

&lt;p&gt;As for the reasons of differences between 3.5 and 4.0., I am not sure. Compiler was changed, indeed. E.g. Covariance and Contravariance in Generics:&lt;br/&gt;
&lt;a href=&quot;http://msdn.microsoft.com/en-us/library/dd799517.aspx&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://msdn.microsoft.com/en-us/library/dd799517.aspx&lt;/a&gt;&lt;/p&gt;</comment>
                            <comment id="44385" author="rstam" created="Mon, 25 Jul 2011 21:21:45 +0000"  >&lt;p&gt;At first I thought this issue was introduced by the implementation of &lt;a href=&quot;https://jira.mongodb.org/browse/CSHARP-276&quot; title=&quot;BsonArray should have constructor/methods dealing with not-generic IEnumerable&quot; class=&quot;issue-link&quot; data-issue-key=&quot;CSHARP-276&quot;&gt;&lt;del&gt;CSHARP-276&lt;/del&gt;&lt;/a&gt;, but apparently it has nothing to do with it. You can get the same compiler error with v1.1 of the driver (as long as you target .NET 3.5 instead of 4).&lt;/p&gt;

&lt;p&gt;Here&apos;s a short sample program that illustrates the issue without using the C# driver at all:&lt;/p&gt;

&lt;p&gt;namespace TestAmbiguousEnumerable {&lt;br/&gt;
    public class C {&lt;br/&gt;
    }&lt;/p&gt;

&lt;p&gt;    public static class Program {&lt;br/&gt;
        public static void Main(string[] args) &lt;/p&gt;
{
            var values = new C[0];
            F(values);
        }

&lt;p&gt;        private static void F(IEnumerable&amp;lt;object&amp;gt; values) {&lt;br/&gt;
        }&lt;/p&gt;

&lt;p&gt;        private static void F(IEnumerable&amp;lt;C&amp;gt; values) {&lt;br/&gt;
        }&lt;/p&gt;

&lt;p&gt;        // commenting out this overload makes no difference&lt;br/&gt;
        private static void F(IEnumerable values) {&lt;br/&gt;
        }&lt;br/&gt;
    }&lt;br/&gt;
}&lt;/p&gt;

&lt;p&gt;This program compiles cleanly when targeting .NET 4 but gives the same compiler error when targeting .NET 3.5:&lt;/p&gt;

&lt;p&gt;Error	1	The call is ambiguous between the following methods or properties: &apos;TestAmbiguousEnumerable.Program.F(System.Collections.Generic.IEnumerable&amp;lt;object&amp;gt;)&apos; and &apos;TestAmbiguousEnumerable.Program.F(System.Collections.Generic.IEnumerable&amp;lt;TestAmbiguousEnumerable.C&amp;gt;)&apos;	c:\users\robert stam\documents\visual studio 2010\Projects\TestAmbiguousEnumerable\Program.cs	14	13	TestAmbiguousEnumerable&lt;/p&gt;

&lt;p&gt;Only the class name has changed.&lt;/p&gt;

&lt;p&gt;I&apos;m going to research this simplified case first. Anyone reading this see why this works with one version of .NET and not another?&lt;/p&gt;</comment>
                            <comment id="44355" author="rstam" created="Mon, 25 Jul 2011 19:13:43 +0000"  >&lt;p&gt;OK. I can reproduce it now. The key to reproducing is: &quot;Target platform: .NET 3.5&quot;.&lt;/p&gt;

&lt;p&gt;My test program was targeted at .NET 4.0, in which case it does compile and run correctly.&lt;/p&gt;</comment>
                            <comment id="44353" author="rstam" created="Mon, 25 Jul 2011 19:10:54 +0000"  >&lt;p&gt;I am unable to reproduce. The following code compiles for me:&lt;/p&gt;

&lt;p&gt;    var values = new BsonValue[] { };&lt;/p&gt;

&lt;p&gt;    var array1 = BsonArray.Create(values);&lt;br/&gt;
    Console.WriteLine(array1.ToJson());&lt;/p&gt;

&lt;p&gt;    var array2 = new BsonArray(values);&lt;br/&gt;
    Console.WriteLine(array2.ToJson());&lt;/p&gt;

&lt;p&gt;    var array3 = new BsonArray();&lt;br/&gt;
    array3.AddRange(values);&lt;br/&gt;
    Console.WriteLine(array3.ToJson());&lt;/p&gt;

&lt;p&gt;and produces the following output:&lt;/p&gt;

&lt;p&gt;[]&lt;br/&gt;
[]&lt;br/&gt;
[]&lt;br/&gt;
Press Enter to continue&lt;/p&gt;

&lt;p&gt;Full source at:&lt;/p&gt;

&lt;p&gt;&lt;a href=&quot;http://www.pastie.org/2269950&quot; class=&quot;external-link&quot; target=&quot;_blank&quot; rel=&quot;nofollow noopener&quot;&gt;http://www.pastie.org/2269950&lt;/a&gt;&lt;/p&gt;</comment>
                    </comments>
                    <attachments>
                    </attachments>
                <subtasks>
                    </subtasks>
                <customfields>
                                                                                                                                                                                                                        <customfield id="customfield_10011" key="com.atlassian.jira.plugin.system.customfieldtypes:radiobuttons">
                        <customfieldname>Backwards Compatibility</customfieldname>
                        <customfieldvalues>
                                <customfieldvalue key="10012"><![CDATA[Major Change]]></customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                <customfield id="customfield_15850" key="com.atlassian.jira.plugins.jira-development-integration-plugin:devsummary">
                        <customfieldname>Development</customfieldname>
                        <customfieldvalues>
                            
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                    <customfield id="customfield_12550" key="com.pyxis.greenhopper.jira:gh-lexo-rank">
                        <customfieldname>Rank</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>2|hrh8nj:</customfieldvalue>

                        </customfieldvalues>
                    </customfield>
                                                                <customfield id="customfield_10558" key="com.pyxis.greenhopper.jira:gh-global-rank">
                        <customfieldname>Rank (Obsolete)</customfieldname>
                        <customfieldvalues>
                            <customfieldvalue>14201</customfieldvalue>
                        </customfieldvalues>
                    </customfield>
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                            </customfields>
    </item>
</channel>
</rss>