D
David
David,
Can you define & give a VB example of "every day" needing strong typing in a
For Each enumeration.
Sure, where MyCollection is a subclass of CollectionBase that holds
Widgets
For each value as String in MyCollection
still compiles. As I said, shadowing GetEnumerator can help alleviate,
but not eliminate the problem.
I can think of one example, however its a
performance/boxing thing which is needed in less then 20% of my code...
How do you see an overridable GetEnumerator providing strong typing?
Umm, because the default GetEnumerator returns an untyped IEnumerator
interface. I honestly don't understand what you're not seeing here.
That's not snarky, I honestly don't understand.
Based
on the performance/boxing requirement I can see an explicit interface
implementation & a strongly typed Enumerator class, again less then 20% of
my code (in fact I have yet to need this optimization).
Nothing I've said has anything to do with performance optimization
(except for a very quick sidebar mention of value type collections a few
posts ago).
Going back to your original post:
I am trying to figure out what & how this "huge hole in the type system" you
perceive is, so that possible I or someone else can correct you. As quite
truthfully I don't see any real holes in CollectionBase, especially when you
overload the CollectionBase.On* methods. Granted you may have runtime errors
as opposed to compile time errors
Okay, I have no idea how to respond to this, since the whole impetus
behind strong typing is so that we can catch errors at compile time
rather than runtime.
Although, when I think about it a little more, I guess this explains
what I didn't understand above. "Strongly typed" has different
definitions (see wikipedia). To me, if a language or feature associates
type with the value but not the variable, that's weakly typed. VBScript
is weakly typed by that definition, although by your definition VBScript
is still strongly typed where COM objects are concerned since they are
typed at runtime. In your words, you may have runtime errors rather
than compile time errors.
If that's your definition though, then we need another term for this
discussion, because I usually want typing that's stronger than that. In
my experience, "strongly typed" in the .Net world usually refers to
compile-time type checking. In fact, didn't you say something just a
few posts ago about ArrayList not being strongly typed, or was that
somebody else?
, however you run that risk (runtime
errors) when using generalized interfaces as IList in any class that
implements IList.
And IList is untyped. Everything's an Object to IList, and so the
compiler can't enforce typing. And if I inherit from CollectionBase I
must accept this and other untyped interfaces, even if I don't want
them. And I seldom do want them unless I'm dealing with certain UI
controls.
I consider IEnumerator more of a problem than IList since the user will
always have to cast things like IList.Item. So I know the user has to
be explicit when the type system is broken, which is finen.
IEnumerator, though, can break strong typing without a cast because of
the way the language works.
Anyway, you seem to have your hackles up, and I have no idea why. I'm
not dissing the writers of CollectionBase here. It's a very useful
class, and given the lack of templates or generics they had little
choice but to allow weakly typed access. Virtual validators were a
pretty clever answer to this problem, but they aren't strong typing.