Menu

#4 get-enum on classes both IEnumerator and IEnumerable

open
nobody
None
5
2008-07-22
2008-07-22
Mark Hurd
No

The built-in definition of get-enum sees a class as IEnumerable and returns it, even if it also implements IEnumerator.

Obviously the code can be changed in Primitives.cs (get_enum_IEnumerator), but the simple override that provides correct behaviour for enumerable enumerators (such as the Linq enumerators) is:

; 'Fix' get-enum so it defaults to
; calling .GetEnumerator even if it
; implements IEnumerator
(def-method (get-enum
(e System.Collections.IEnumerator.))
(if (is? e System.Collections.IEnumerable.)
(.System.Collections.IEnumerable:GetEnumerator e)
e))

I've added the above to my personal extra.lisp (with some extra tracing to see how often it is exercised) and it hasn't broken anything yet.

It /will/ break something if existing code starts an enumeration and calls something else with the 'active' enumerator and that calls get-enum again, where the enumerator still implements IEnumerable.

Discussion


Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.