VB -> C#: Generic extension method inference widens element types when using a base-type comparer
Mantenedores costumam responder em até 3 dias
Ninguém assumiu esta issue ainda.
Avaliação
- Dificuldade
- 4/5
- Tempo estimado
- 3-5 dias
- Facilidade para iniciantes
- 55/100
Direção de pesquisa
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.
Escrita pelo modelo de indexação a partir do texto da issue.
Descrição
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)
- Linguagem predominante
- C#
- Estrelas
- 917
- Forks
- 243
- Merge médio
- 2d 15h
- PRs com merge (30d)
- 9
Preparar o ambiente
- Sem Dockerfile nem arquivo Docker Compose
- Tem um modelo de pull request
- Ler o guia de contribuição
Primeiros passos
- Leia a issue inteira e depois o guia de contribuição do projeto.
- Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
- Faça um fork do repositório e trabalhe em uma branch.
- Abra um pull request que referencie o número da issue.
Mais de icsharpcode/CodeConverter
-
VB -> C#
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 72/100
icsharpcode/CodeConverter#1301 ·
Mantenedores costumam responder em até 3 dias
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 78/100
icsharpcode/CodeConverter#1271 · 1 comentário ·
Mantenedores costumam responder em até 3 dias
-
VB -> C#
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 68/100
icsharpcode/CodeConverter#1300 ·
Mantenedores costumam responder em até 3 dias
-
VB -> C#
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 62/100
icsharpcode/CodeConverter#1299 ·
Mantenedores costumam responder em até 3 dias
-
VB -> C#
Dificuldade 3/5 1-2 dias Facilidade para iniciantes 62/100
icsharpcode/CodeConverter#1298 ·
Mantenedores costumam responder em até 3 dias
Todas as issues de icsharpcode/CodeConverter
Issues semelhantes
-
The Recommended preset turns on Import Uno DSP, which only works with the Material themeTalvez já em andamento @agneszitte assumiu hoje. Aberta
Dificuldade 1/5 Menos de uma hora Facilidade para iniciantes 92/100
unoplatform/uno.templates#2388 ·
Mantenedores costumam responder em até 1 dia
-
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 77/100
eyalgolan/statecraft-israel#2732 ·
Mantenedores costumam responder em até 1 dia
-
変換の候補のどれが Zenzai の答えか分からないAberta要トリアージ
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 64/100
Mantenedores costumam responder em até 1 dia
-
kind/chore status/draft
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 65/100
Altinn/altinn-events#1158 ·
Mantenedores costumam responder em até 1 dia
-
S: Untriaged
Dificuldade 2/5 1-3 horas Facilidade para iniciantes 62/100
Trauma-Station/Trauma-Station#5285 ·
Mantenedores costumam responder em até 1 dia