Programming without Types
Warning, actual serious content. This is not a joke. Repeat, this is not a joke.
I don't want to get rid of types. I just don't want to have to explicitly say them all the time. I want more type inference in the language. Again, this is not some lead in to another silly post making fun of type inference. I actually like type inference to some degree. I want more of it.
Where do I want it? First off, I want it in my foreach statement. Why do I have to specifically call out the type of my iteration variable. I should be able to deduce it from the collection. Sure, back in olden days of .Net you could not determine the appropriate element type unless you had written up a special collection class with a special strongly-typed enumerator. But now, in the next release everything is generics. You've got generic interfaces for IEnumerable/IEnumerator. They always know the element type, always. So, foreach, my friend, why do I still have to re-state the element type?
Instead of: foreach(Foo f in foos) { ... }
Why not: foreach(f in foos) { ... }
The local 'f' is still strongly typed.
Better right? Well, I did after all convert the X# compiler to work this way. Maybe one day you'll see it in C-omega, whenever that research compiler finds it way into the wild. It would be fantastic, IMO, if C# could do this.
Where else would you get rid of typing the type in C#, if you could?
Matt
Comments
- Anonymous
June 10, 2004
The comment has been removed - Anonymous
June 10, 2004
You're forgetting one thing - your NOW is definitelly not now, and probably not even in the near future (few months). I want people to handle issues in an existing product, not be focused on something that's not released.
As for not using types - why not? The foreach loop variable doesn't have to be of the same type that the collection holds, not even with generics. Let's say you want to serialize all elements in the collection, so you iterate ISerializable type, not the actual element type. - Anonymous
June 10, 2004
The comment has been removed - Anonymous
June 10, 2004
I was not suggesting getting rid of the ability to express the type, just making it optional in some cases. Not specifying the type in a local variable declaration/initialization makes it hard to figure out that what you are looking at is actually a local variable declaration/initialization. So that wouldn't be good. Though maybe there is another way that is good. - Anonymous
June 10, 2004
The comment has been removed - Anonymous
June 10, 2004
The comment has been removed - Anonymous
June 10, 2004
With VB in that mode, nothing is actually inferred. It is just untyped. - Anonymous
June 10, 2004
I showed this to someone else and the pointed out a good point about the fact that I was taking it too far to the extreme. And, Matt points out that it's untyped... so...
Dim integers As New IntegerCollection
integers.Add(1)
integers.Add(2)
integers.Add(3)
For Each i In integers
..If TypeOf i Is Int16 Then
....MsgBox(i)
..End If
Next
You will see 3 message boxes appear... and at no point did I ever say that i was an int16.
The IntegerCollection is a sample Int16 collection taken directly from the online help when you look up CollectionBase.
Note, periods added to preserve tabs ;-) - Anonymous
June 10, 2004
Woah! That's it. VB has it, and I want it. Maybe I signed up for the wrong gig. Maybe VB is the holy land after all. - Anonymous
June 10, 2004
;-) - Anonymous
June 10, 2004
Sounds like maybe you want to use a ML language like O'CaML, or SML.NET or F#. Those languages infer the type at compile time, like you would like C# to do. - Anonymous
June 10, 2004
I can remember reading an article bundled on an old (VB6) tech cd about using variants (VB's untyped datatype) for EVERYTHING.
I started off thinking, this man is MAD and finished up agreeing with him completely.
One of the best examples I can think of is handling Nulls. You can't store (or test for) a Null in a regular typed variable and an empty string is not the same as a Null.
These days, I develop mainly for the web in PHP which is also untyped and I can't say I miss typed variables much. You don't even have to declare their usage in advance, just get on and write the code. - Anonymous
June 10, 2004
C++ is exploring some of these ideas; see http://www.open-std.org/JTC1/SC22/WG21/docs/papers/2004/n1607.pdf - Anonymous
June 11, 2004
The comment has been removed