Wiki

Ticket #163 (closed enhancement: fixed)

Opened 8 years ago

Last modified 8 years ago

Inferring types related to System.Text.RegularExpressions

Reported by: webnov8 Owned by: Chuck
Priority: minor Milestone: Cobra 0.9
Component: Cobra Compiler Version: 0.8.0
Keywords: cobra type inferrence regular expressions system.text.regularexpressions Cc:

Description

Currently when you try enumerating over a MatchCollection? the type returned is Object?

For e.g. let's say we wanted to find all occurrences of @param in a file...

str = '' # some string
for match in Regex.Match(str, r'^\s*@param\s+(.*)$')
  # we expect a string to be located in the group identified by (.*)
  groups = match.groups # this will not work because match is of type Object?

Specifying the type works as expected as in...

for match as Match in Regex.Match(...)

Attachments

infer-matchCollection-item.patch Download (2.8 KB) - added by hopscc 8 years ago.

Change History

Changed 8 years ago by Chuck

The problem is that MatchCollection? implements IEnumerable, but not IEnumerable<of T>. The idea for fixing this is to look at the indexer for the type and use its return value. This problem comes up for other .NET library classes that were built prior to .NET 2.0 generics.

It would be interesting to know if C# 3.0's var can useful inference on this.

Changed 8 years ago by torial

FWIW, when I tried it in C# 3, it wouldn't compile and gave me:

'object' does not contain a definition for 'Groups' and no extension method 'Groups' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?)

Changed 8 years ago by hopscc

Changed 8 years ago by hopscc

  • owner set to Chuck
  • status changed from new to assigned

Example above should be referring to Regex.matches. ( returns MatchCollection)
Similar issue with GroupCollection.

Patch to support this includes test case and release note.
Fix is to do additional check if IEnumeration result type is Object.
Look at indexer ([]) return type and provide that if its not Object.

Changed 8 years ago by Chuck

  • status changed from assigned to closed
  • resolution set to fixed

Applied in changeset:2358. In changeset:2359 I switch up the code to use Box.isSystemObjectClass to avoid picking up a user class named 'Object'.

Note: See TracTickets for help on using tickets.