OT(Slightly): Thanks to Python.

Jacek Generowicz jacek.generowicz at cern.ch
Wed Mar 10 10:03:31 EST 2004


JanC <usenet_spam at janc.invalid> writes:

> Jacek Generowicz <jacek.generowicz at cern.ch> schreef:
> 
> > - Note that item is bound to objects of completely different types
> >   (integer, string, list, hash-table and array, in this case) at
> >   different times. (This _does_ mean that it's dynamically typed.)
> 
> Yes, but 2 of the "method versions" use statically typed parameters.

What gives you that idea? There are no statically typed parameters in
anything I have shown you (except the C++).

  (defmethod foo ((x integer))
    (format t "~&~s is an integer." x)
    (setq x "this is a string")
    (format t "~&Oh look, x was an integer, and now it's a string: ~s." x))
  
  (foo 2)

output:

  2 is an integer.
  Oh look, x was an integer, and now it's a string: "this is a string".

If x were a statically typed parameter, then it would not be possible
for it to "be" an integer at one moment, and a string a moment later.

I think you really should tell me what _your_ definition of static
typing is, otherwise we're going to go around in circles for a very
long time.

> So it seems this is a language that gives you the choice between dynamic 
> and static typing.

As it happens, this language does give you the option of giving type
declarations, which serve as optimization hints to the compiler (which
is not the same as static typing) ... but what I have shown you
(method dispatch) has nothing to do with type declarations.

> Without this (optional) static typing in the language, there is no way to 
> have these method versions for different parameter types. 

Oh really?  Here's a counterexample in Python:

class generic_function:

    def __init__(self):
        self.methods = {}

    def add_method(self, method, *types):
        self.methods[types] = method

    def __call__(self,*args):
        types = tuple(map(type,args))
        return self.methods[types](*args)

foo = generic_function()

def _foo(x):
    print '%s is a string' % x
foo.add_method(_foo, str)

def _foo(x):
    print '%s is an integer' % x
foo.add_method(_foo, int)

foo(2)        # =>  2 is an integer
foo('hello')  # =>  hello is a string

[It could do with a bit of syntactic sugar to make it look more like
normal method definitions, but that's all it takes to implement. Now,
in our mystery language we could add the syntactic sugar ourselves,
trivially; in Python we probably need to ask the language implementors
very nicely (i.e. wire a PEP, defend it, provide a reference
implementation (this means hacking the Python interpreter
itself)). None of this, however, means that it can't be done in a
language without static typing.]

> I suppose that's what John Roth was thinking about when he said "but
> there's no way it's going to happen in a language that doesn't have
> static typing".

I am afraid that John Roth was just plain wrong. 

(Caveat, if you try to twist the meaning of those words until you get
to some sufficiently meaningless interpretation, then you might,
eventually, find some way of reading them that makes him right.)



More information about the Python-list mailing list