[Types-sig] VB Types

Paul Prescod paul@prescod.net
Thu, 02 Dec 1999 20:53:24 -0600


Guido van Rossum wrote:
> 
> > Here's an approach that we didn't try because it is likely to be wildly
> > unpopular:
> 
> Why would it be unpopular?

Stealing ideas from Visual Basic? Shudder!

> Do you have the time to describe this in somewhat more detail for us
> lucky folks who haven't had the pleasure to learn VB?

The declarations are totally optional. If you don't declare something
then it is a "Variant" which is a grab-bag like void * or PyObject. So
the semantics of an untyped program are similar to Pythons:

Private Function Foo()
    b = "foo"
    MsgBox (b)
    b = 5
    MsgBox (b)
End Function
    
b is a Variant. So is the return value of the function. I could change
that:

Public Function Foo() As Slide
        Set Foo = ActivePresentation.Slides(0)
End Function

Ignore the word "set". It's a hack and I think that even in VB there
isn't a good reason it is necessary. 

Their word for "declare" is "dim"

Dim i as Integer

As soon as you Dim something, the IDE tries to help you with its method
signatures. That's useful enough to encourage type declarations for
things of known type...which in turn can help you catch type errors more
quickly. I've prototyped some COM apps in VB and then port to Python
because the method signatures stuff is important when the COM object
isn't well documented (usually!).

For some reason, it isn't compile time type safe. This would cause a
runtime error:

    Dim b As Integer
    MsgBox (b)
    b = "foo"
    b = 5

I don't think I've ever got a compile time type error message. Perhaps
they don't want to give a false sense of "type safety" because it is
still very possible to make type errors (because of the variants).

Still, in a Python implementation I would expect IDEs to have a "check
all types" menu item and the Python interpreter would also need a check
all types command line option. The default value of integers is "0". 

Parameters can be typed or implicitly variant:

Public Function Foo(a As Integer, b, c as String) As Collection
    Set Foo = New Collection
End Function

Classes are types so you can create new types easily. There is no
concept of predefined interfaces (other than interfaces in a typelib)
but that could be added easily. There is no union type: you would have
to use variants.

As you point out, these same definitions can be used to interface to
statically typed languages without good introspection and libraries but
that also depends on built-in language features.

I can't think of anything else that is relevant.

-- 
 Paul Prescod  - ISOGEN Consulting Engineer speaking for himself
"I always wanted to be somebody, but I should have been more
specific." --Lily Tomlin