| Store | Cart

OT(Slightly): Thanks to Python.

From: Jacek Generowicz <jace...@cern.ch>
10 Mar 2004 16:03:31 +0100
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)


  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.)

Recent Messages in this Thread
Aahz Mar 10, 2004 10:48 pm
Bob Ippolito Mar 04, 2004 11:51 pm
David M. Cook Mar 04, 2004 02:57 pm
Ville Vainio Mar 04, 2004 03:29 pm
John Roth Mar 04, 2004 04:37 pm
Michael Hudson Mar 04, 2004 07:40 pm
Christopher Koppler Mar 05, 2004 09:03 am
Jacek Generowicz Mar 05, 2004 09:43 am
JanC Mar 05, 2004 05:16 pm
Jacek Generowicz Mar 08, 2004 09:24 am
JanC Mar 09, 2004 09:25 pm
Michael Hudson Mar 10, 2004 11:47 am
Jacek Generowicz Mar 10, 2004 03:03 pm
JanC Mar 13, 2004 05:24 am
Josiah Carlson Mar 13, 2004 07:17 am
Ville Vainio Mar 13, 2004 09:08 am
Ville Vainio Mar 13, 2004 09:17 am
David M. Cook Mar 05, 2004 06:32 am
David M. Wilson Mar 10, 2004 08:34 pm
Messages in this thread