dotnet / dotnet/vblang

[Proposal] Ease the pain of using non-generic (object) collections in For Each

Open
#523 9 comments 1 reaction 0 assignees View on GitHub
Dominant language
No language data
Stars
328
Forks
71
PR merge metrics
No merged PRs in 30d

Description

I have this code:
```
Protected Function GetAlwaysVisibleItems() As IEnumerable(Of CompletionItem)
Return From elm As XmlSchemaElement In HtmlSchema.Elements.Values
Select GetItem(elm.Name)
End Function
```

And I get the error:
> Option Strict On disallows implicit conversions from 'Object' to 'XmlSchemaElement'.

I have two solutions:
1- Use `Option Strict Off` which I don't want to do for all the file.
2- Use explicit cast
```VB.NET
Protected Function GetAlwaysVisibleItems() As IEnumerable(Of CompletionItem)
Return From elm In HtmlSchema.Elements.Values
Select GetItem(CType(elm, XmlSchemaElement).Name)
End Function
```

which is a long of unnecessary code.

I need two things here:
1. A way to make VB accept the implicit casting in for each. If VB will not do that by default , maybe we can use `[]` around the type as an indication.
```VB.NET
Protected Function GetAlwaysVisibleItems() As IEnumerable(Of CompletionItem)
Return From elm As [XmlSchemaElement] In HtmlSchema.Elements.Values
Select GetItem(elm.Name)
End Function
```

this syntax is accepted today as the `[]` are used for keyword dis-ambiguity. If this is not acceptable for some consideration, we can instead use it around `As type`:
```VB.NET
Protected Function GetAlwaysVisibleItems() As IEnumerable(Of CompletionItem)
Return From elm [As XmlSchemaElement] In HtmlSchema.Elements.Values
Select GetItem(elm.Name)
End Function
```

2. I want VB to infer the non-generic collection's element type from its indexer. I know that may break existing code, so what I am asking if to auto add `[As XmlSchemaElement]` in the for each / from , or offer it as the suggested type in auto complete.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.