Why doesn't the compiler use ToString for implicit conversions?

M

Mike S

Maybe I'm missing some fundamental unwritten law of OOP, but I was
wondering why the VB.NET compiler doesn't take advantage of the fact
that all .NET objects, being derived from the Object base class, have
a ToString method defined on them, when a reference type is used in an
expression where a String is expected. I mean if you define a class
MyClass in MyNamespace and don't override ToString, calling ToString
will return the qualified type name: "MyNamespace.MyClass" (the
default behavior). If you then do this:

Dim myObject As MyNamespace.MyClass
Dim str As String

str = "MyClass converted to a string = " & myObject

The compiler will complain that myObject cannot be cast to String. The
compiler obviously knows what types are involved in any potential
conversion, so why can't it just call myObject.ToString to perform the
conversion on realizing that myObject should be converted to String?
In another OOP language this might not make sense, since the compiler
can't assume a method called ToString exists on any given object, but
since .NET objects by definiton must all inherit from the common
Object base class, and ToString is part of Object's defintion, why
shouldn't the compiler just call ToString, since it knows the method
will be there?

Now, obviously, you can easily get around all this by explicitly
calling ToString yourself, but I'm wondering why the compiler won't
simply do it automatically (perhaps it could perform it only as long
if Option Strict is Off to give the programmer a choice).

I mean, the compiler will implicitly convert a value type into a
String when necessary, why not extend this to reference types in the
specific case of converting to String, since, again, the ToString is
readily available?

I guess what I'm getting at is this: is there a good reason why it
doesn't work this way? Especially when you consider that the following
code works without needing to explicitly call myObject.ToString:

Console.WriteLine(myObject)

I assume this works because the parameters to WriteLine are of type
Object, and my guess is that WriteLine simply calls ToString on the
objects passed to it to get a String suitable for output. So, it's
more to do with how WriteLine is implemented rather than a situtation
where a compiler-defined conversion occurs, but what's stopping the
compiler from doing the same thing in the specific case of converting
a reference type to String?
 
P

Peter Macej

I don't know the right reasons for that. But one of them might be the
fact that you can redefine ToString so that it doesn't return string:

Class MyClass
''' <summary>Crazy ToString.</summary>
Public Shadows Function ToString() As Collection
Return New Collection
End Function
End Class

And I think the "Shadows" keyword is not even necessary here.
 
M

Mattias Sjögren

I don't know the right reasons for that. But one of them might be the
fact that you can redefine ToString so that it doesn't return string:

But you (and the compiler) can still call the base definition.

C# supports this so its definitely doable.


Mattias
 
M

Michael D. Ober

By requiring the use of the ToString() method in code, the VB 2005 compiler
eliminates an entire class of possible bugs caused by incorrect type
casting. Yes, C# can do this, but IMO doing so leads to harder to read
code.

Mike Ober.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top