VB -> C#: Generic extension method inference widens element types when using a base-type comparer
Les mainteneurs répondent en général sous 4 jours
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 55/100
Piste de recherche
Start from the VB-to-C# conversion path for generic extension-method calls and reproduce the issue with the VB input in the report. Compare the generated Distinct call with the expected output; done means it specifies DerivedValue and preserves the declared return type.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
VB.Net input code
Imports System.Collections.Generic
Imports System.Linq
Public Class BaseValue
Public Property Key As Integer
End Class
Public Class DerivedValue
Inherits BaseValue
End Class
Public Module Helpers
Public Function ToDictionary(values As IEnumerable(Of DerivedValue)) As Dictionary(Of Integer, DerivedValue)
Return values.Distinct(EqualityComparer(Of BaseValue).Default).ToDictionary(Function(value) value.Key)
End Function
End Module
Erroneous output
The converted method leaves the generic arguments of Distinct implicit:
public static Dictionary<int, DerivedValue> ToDictionary(IEnumerable<DerivedValue> values)
{
return values.Distinct(EqualityComparer<BaseValue>.Default).ToDictionary(value => value.Key);
}
C# infers BaseValue as the element type of Distinct, so ToDictionary returns Dictionary<int, BaseValue>.
This produces:
CS0029: Cannot implicitly convert type
'System.Collections.Generic.Dictionary<int, BaseValue>'
to 'System.Collections.Generic.Dictionary<int, DerivedValue>'
Expected output
The converter should preserve the type argument inferred by VB.NET:
public static Dictionary<int, DerivedValue> ToDictionary(IEnumerable<DerivedValue> values)
{
return values.Distinct<DerivedValue>(EqualityComparer<BaseValue>.Default).ToDictionary(value => value.Key);
}
Details
- Product: Visual Studio extension, built locally from master.
- Version: 10.0.1 (local development build).
- Previous working version: No.
VB.NET and C# use different type inference rules for generic extension methods.
VB.NET first infers the relevant type parameters from the receiver. In this example, it fixes the element type as DerivedValue. The base-type comparer is then accepted through the contravariance of IEqualityComparer(Of T).
C# inference considers both the receiver and the comparer, resulting in BaseValue instead. Omitting explicit type arguments therefore changes the type of the resulting sequence.
Microsoft's VB team describes this receiver-first inference here:
https://devblogs.microsoft.com/vbteam/extension-methods-and-generics-extension-methods-part-5/
A workaround is to specify the type argument explicitly in the VB source:
Return values.Distinct(Of DerivedValue)(EqualityComparer(Of BaseValue).Default).ToDictionary(Function(value) value.Key)
- Langage dominant
- C#
- Étoiles
- 917
- Forks
- 243
- Merge moyen
- 4 j 42 min
- PR mergées (30 j)
- 11
Préparer son environnement
- Aucun Dockerfile ni fichier Docker Compose
- Propose un modèle de pull request
- Lire le guide de contribution
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de icsharpcode/CodeConverter
-
VB -> C#
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
icsharpcode/CodeConverter#1301 ·
Les mainteneurs répondent en général sous 4 jours
-
Difficulté 2/5 1-3 heures Accessibilité débutants 78/100
icsharpcode/CodeConverter#1271 · 1 commentaire ·
Les mainteneurs répondent en général sous 4 jours
-
VB -> C#
Difficulté 3/5 1-2 jours Accessibilité débutants 68/100
icsharpcode/CodeConverter#1300 ·
Les mainteneurs répondent en général sous 4 jours
-
VB -> C#
Difficulté 3/5 1-2 jours Accessibilité débutants 62/100
icsharpcode/CodeConverter#1299 ·
Les mainteneurs répondent en général sous 4 jours
-
VB -> C#
Difficulté 3/5 1-2 jours Accessibilité débutants 62/100
icsharpcode/CodeConverter#1298 ·
Les mainteneurs répondent en général sous 4 jours
Toutes les issues de icsharpcode/CodeConverter
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 82/100
NethermindEth/nethermind#15684 ·
Les mainteneurs répondent en général sous 1 jour
-
area/ai-assistant squad/flyt
Difficulté 2/5 1-3 heures Accessibilité débutants 83/100
Altinn/altinn-studio#21798 ·
Les mainteneurs répondent en général sous 1 jour
-
bug
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
arastella625/DAORework#181 ·
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 77/100
BEagle1984/silverback#280 ·
-
bug P1
Difficulté 2/5 1-3 heures Accessibilité débutants 76/100
endercodezz/BYDLO#417 ·
Les mainteneurs répondent en général sous 1 jour