in reply to Static typing is mostly a waste of time

Perl's type system is both static and very strong. It just contains very few types - scalar, hash, array, filehandle, code. (And format, but no-one uses it anymore.) You cannot convert between them and they're set at compile time.

Now, the big reply is "But scalars are strings and numbers and Perl implicitly converts between them." Yes, it does. However, look at it this way - there is a type called scalar. There are dozens of operators that work on scalars. Some of them work with the scalar one way and some work with it another way. The fact they treat the scalar differently is both consistent and documented. It doesn't change the fact that it's still a scalar and it was typed as a scalar at compile time and it will never change what it is. Static and strong typing.

Another reply is "But when you do @foo + $bar, @foo gets converted to a scalar." Well, yes, that's what it looks like. But, + is defined differently for @foo than it is for $bar. It's not conversion - it's polymorphism. :-)

  • Comment on Re: Static typing is mostly a waste of time

Replies are listed 'Best First'.
Re^2: Static typing is mostly a waste of time
by Anonymous Monk on Apr 12, 2005 at 15:37 UTC
    Perl's type system is both static and very strong. It just contains very few types - scalar, hash, array, filehandle, code. (And format, but no-one uses it anymore.) You cannot convert between them and they're set at compile time.
    That's an interesting assertion...
    @a=("abc",1,"42","dolphin"); %b=@a; print "$_,$b{$_}\n" for keys %b;
      Assignment is also polymorphic. %b (or @b, for that matter) forces '=' to impose list context upon @a. In other words, '=' changes behavior depending on the LHS. It's not type-casting, which is what I meant to say.
Re^2: Static typing is mostly a waste of time
by Anonymous Monk on Apr 12, 2005 at 22:21 UTC
    So is assembly language strongly/statically typed?
    The Assembly language type system is both static and very strong. It just contains only one type - bytes. (And octets, but no-one uses them anymore.)

    Now, the big reply is "But bytes can be strings and numbers and Assembly language implicitly converts between them." Yes, it does. However, look at it this way - there is a type called bytes. There are dozens of operators that work on bytes. Some of them work with the bytes one way and some work with it another way. The fact they treat the bytes differently is both consistent and documented. It doesn't change the fact that it's still a byte and it was typed as a byte at compile time and it will never change what it is. Static and strong typing.

      A type system with just one type is both static and strong. It's just not very useful as a type system.
        But by the definition above, doesn't every language qualify as statically/strongly typed? Couldn't we have used tcl, or fortran or basic in the example above and come to the exact same conclusion? Doesn't the term "statically/strongly typed" loose its meaning if *every* language meets the criteria? Can you give us an example of a language that's not statically/strongly typed?
Re^2: Static typing is mostly a waste of time
by Anonymous Monk on Apr 12, 2005 at 14:35 UTC
    Neither does @foo get converted to a scalar, nor is + defined differently for @foo than it is for $bar.

    The value of @foo is a number in scalar context, but it's not a conversion - @foo doesn't change. Nor is + internally a huge dispatch table, doing all kinds of different things depending on the types of its arguments. Binary + always gets two scalars as arguments, and isn't doing anything before the scalar values are known.

      I said: + is defined differently for @foo than it is for $bar. It's not conversion - it's polymorphism.

      How that polymorphism is achieved is irrelevant. '+' will do the right thing when handed varying types. The fact that '+' imposes scalar context upon its operands and @foo in scalar context is "$#foo + 1" doesn't change the fact that it's '+' which is the agent of the context conversion.

        You're absolutely right. If you define polymorphism to be that, then indeed, it's polymorphism.